********************************************************************************
*	PROJECT: Childhood confidence and long-term outcomes (PSID)
*	PURPOSE: Clean CDS files 
*	PUBLISHED: August 2022
*	CONTACT: Hannah Ruebeck, hruebeck@mit.edu
********************************************************************************
clear all
set more off
set maxvar 10000

local path "/Users/XXXXX/Downloads/replication materials"

local RAW "`path'/raw"
local INDIV "`RAW'/Cross_year_indiv"
local CLEAN "`path'/clean"

/*
Description: This code loads in each year's CDS file and cleans the variables, then
appends all years together using the 1968 interview and person ID numbers which are 
included in the data. Variables that indicate primary and other caregivers' unique identifiers 
are also merged in. The output is a file called "cleaned_cds.dta", stored in the "clean" folder.
*/

********************************************************************************
// PROGRAMS TO HELP WITH CLEANING

program switch_varname_and_varlabel
    syntax, var(varname) prefix(string)
    local label: variable label `var'
    local newvar = lower("`label'")
    local newvar = subinstr("`newvar'", " 97", "", .)
    local newvar = subinstr("`newvar'", " 02", "", .)
    local newvar = subinstr("`newvar'", " 07", "", .)
    local newvar = subinstr("`newvar'", "-", "", . )
    local newvar = subinstr("`newvar'", "(", "_", . )
    local newvar = subinstr("`newvar'", ")", "", . )
    local newvar = subinstr("`newvar'", "/", "_", . )
    local newvar = subinstr("`newvar'", "#", "num", . )
    local newvar = subinstr("`newvar'", "'", "", . )
    local newvar = subinstr("`newvar'", "&", "and", . )
    local newvar = subinstr("`newvar'", ",", "_", . )
    local newvar = subinstr("`newvar'", "$$", "money", . )
    local newvar = subinstr("`newvar'", "<", "under", . )
    local newvar = subinstr("`newvar'", ".", "", . )
    local newvar = subinstr("`newvar'", "=", "equal", . )
    local newvar = subinstr("`newvar'", ":", "_", . )
    local newvar = subinstr("`newvar'", "?", "", . )
    local newvar = subinstr("`newvar'", "%", "pct", . )
    local newvar = subinstr("`newvar'", "calss", "class", . )
    local newvar = subinstr("`newvar'", "2nd", "second", . )
    local newvar = subinstr("`newvar'", "3rd", "third", . )
    local newvar = subinstr("`newvar'", "4th", "fourth", . )
    local newvar = subinstr("`newvar'", "5th", "fifth", . )
    local newvar = subinstr("`newvar'", "6th", "sixth", . )
    local newvar = subinstr("`newvar'", "7th", "seventh", . )
    local newvar = subinstr("`newvar'", "8th", "eigth", . )
    local newvar = subinstr("`newvar'", "9th", "ninth", . )
    local newvar = subinstr("`newvar'", "10th", "tenth", . )
    local newvar = subinstr("`newvar'", "11th", "eleventh", . )
    local newvar = subinstr("`newvar'", "12th", "twelfth", . )
    local newvar = subinstr("`newvar'", "10+", "over10", . )
    local newvar = subinstr("`newvar'", "+", "pos", . )
    local newvar = subinstr("`newvar'", ">", "more", . )
    local newvar = subinstr("`newvar'", "aggravation", "agg", . )
    local newvar = subinstr("`newvar'", " ", "", .)
    if strpos("`newvar'", "time")>0 local newvar = subinstr("`newvar'", "_military", "", .)
    if strpos("`newvar'", "time")>0 local newvar = subinstr("`newvar'", "_milit", "", .)
    local newshort = substr("`prefix'_`newvar'", 1, 32)
    rename `var' `newshort'
    label variable `newshort' "`var'"
    
end

label define indicator 1 "Yes" 0 "No"
program label_indicator
    syntax, var(varname)
    label values `var' indicator
    local label: variable label `var'
    if strpos("`label'", "NOT IN")>0 label variable `var' "`label'"
    else label variable `var' "Based on `label'"
end

********************************************************************************
// BRING IN INDIVIDUAL CROSS-YEAR DATA TO GET GENDER

do "`INDIV'/ind2017er/IND2017ER.do"
gen uid = string(ER30001, "%04.0f") + string(ER30002, "%03.0f")
keep uid ER32000
tempfile indiv_cross_year
save `indiv_cross_year'

********************************************************************************
// BRING IN, CLEAN, AND MATCH CDS WAVES
* List of numbers associated with each years download of CDS
local filelist 265631 269467 269469
* List of interview number variables in each year
local intnumlist 33401 33601 33901
* List of sequence number variables in each year
local seqnumlist 33402 33602 33902

local W = 1
forval year =1997(5)2007 {
    foreach w in file intnum seqnum {
	local `w': word `W' of ``w'list'
   }
	
	* bring in the data
	do "`RAW'/CDS`year'/J`file'.do"
	do "`RAW'/CDS`year'/J`file'_formats.do"
	  
	***********************
	** variable cleaning **
	***********************
	* id numbers and interview completion variables
	gen uid = string(ER30001, "%04.0f") + string(ER30002, "%03.0f")
	rename ER`intnum' cds_int_num_`year'
	rename ER`seqnum' cds_seq_num_`year'
	order uid, first
	label var uid "Unique ID: ER30001+ER30002"

	* year-specific cleaning and renaming of variables
	if `year'==1997 {
	    
	    gen haschiw = !mi(Q3LW1)
	    gen haspcgciw = 1
	    gen haspcghiw = !mi(Q2A1)
	    
	    drop Q1C* Q1D* Q1E* Q1F* Q1B17 Q1G1 Q1G9  Q1G18 Q3C0 Q1H2_1-Q1H39 Q1J1-Q1J30F ///
		Q2CHECK-Q2MIN4 Q6CHECK-Q6MIN4 IMPUTED* EMIMP* DIARY97 DEMREL97 CHREL97 ///
		HHREL97 Q2BOOKS CHLREL97 OCGCRL97 OCGHRL97 EMSTRL97 WD973901-WD_T1 ///
		Q7A0 Q7B0T Q7B7T Q7B20 Q8A0 Q8B0 Q8C10 Q9D6_*B Q9D6_*E Q9D9_*B Q9D9_*E ///
		Q9D12_*B Q9D12_*E Q10B6?B Q10B6?E Q10B9?B Q10B9?E  Q10B12?B Q10B12?E 
	    	    
	    label variable Q3LW17 "LW TEST ITEM 17 97"
	    label variable Q6A19G "LIVE TOG BEFORE MARRIAGE 97"
	    label variable Q10A14L "OTHER SERVICES"
	    label variable Q10A30G "OTHER CERTIFICATION"
	    label variable Q11A16F "OTHER RACE"
	    label variable Q11A21E "OTHER MEAL"
	    label variable Q11A40G "OTHER CERTIFICATION"
	    label variable Q11A41E "OTHER TRAINING"
	    label variable Q12A2P "OTHER GRADES"
	    label variable Q12A15 "NUMBER TEACHERS WORK FULL TIME"
	    label variable Q12A17 "NUMBER TAS WORK FULL TIME"
	    label variable Q13A22A "NUMBER OF CHILDREN PAID FOR"
	    label variable Q13A23 "NUMBER OF CHILDREN USE VOUCHERS"
	    label variable Q13A20L "OTHER SPONSOR"
	    label variable Q13A22E "OTHER PAYMENT TYPE"
	    label variable Q13A24E "OTHER MEALS"
	    label variable Q13A35C "NUMBER OTHER TRAINING"
	    label variable Q13A34HN "NUMBER - OTHER DEGREES"
	    label variable Q13A34HP "PERCENT - OTHER DEGREES"
	    label variable Q13A47H "PERCENT OTHER FUNDS"
	    local i = 1
	    foreach l in A B C D E {
	        label var Q10B6`l' "MORNING ACTIVITY `i'"
	        label var Q10B9`l' "AFTERNOON ACTIVITY `i'"
	        label var Q10B12`l' "EVENING ACTIVITY `i'"
	        local i = `i'+1  
	    }
	     
	    * renaming variables by their labels which are more consistent across years:
	    foreach var of varlist Q1A1-Q13A47H {
	        if "`var'"=="Q1A1" local prefix "pcgc" // primary caregiver child file
		if "`var'"=="Q2A1" local prefix "pcgh" // primary caregiver household file
		if "`var'"=="Q3AGE" local prefix "chld" // child file
		if "`var'"=="Q5A1_1" local prefix "ocgc" // other caregiver child file
		if "`var'"=="Q6A1_1" local prefix "ocgh" // other caregiver household file
		if "`var'"=="Q7A1" local prefix "fothc" // father outside the home child file
		if "`var'"=="Q8A1" local prefix "fothh" // father outside the home household file
		if "`var'"=="Q9A1M" local prefix "emtch" // elem or middle school teacher file
		if "`var'"=="Q10A1M" local prefix "pstch" // preschool teacher file
		if "`var'"=="Q11A1M" local prefix "hbcp" // home-based care provider file
		if "`var'"=="Q12A1" local prefix "emadm" // elem or middle school administrator file
		if "`var'"=="Q13A1" local prefix "psadm" // preschool administrator file
		
		if strpos("`var'", "Q13A16")==1 {
		    local label: variable label `var'
		    local newlabel = subinstr("`label'", " - ", " CHILDREN - ", 1)
	            label variable `var' "`newlabel'"
	        }
	        if strpos("`var'", "Q13A33")==1 {
	            local label: variable label `var'
	            local newlabel = subinstr("`label'", " - ", " STAFF - ", 1)
	            label variable `var' "`newlabel'"
	        }
		
		switch_varname_and_varlabel, var(`var') prefix(`prefix')
	    }
	    rename ER33403 reltohead
	    foreach var of varlist reltohead-BIOGPR97{
	        rename `var' dem_`var'
	        rename dem_`var', lower
	    }
	     
	    * cleaning up some demographic info that doesn't appear in other years
	    gen dem_childofheads = inlist(dem_reltohead, 30, 33, 35, 38, 83)
	    label var dem_childofheads "Based on ER33403"
	    replace dem_newimmi = 0 if dem_newimmi==5
	    label_indicator, var(dem_newimmi)
	    rename dem_newimmi dem_immigfam
	    replace pcgc_borninusa = . if pcgc_borninusa==9
	    replace pcgc_borninusa = 0 if pcgc_borninusa==5
	    label_indicator, var(pcgc_borninusa)
	    rename pcgc_borninusa dem_immigchild
	    rename dem_sblnum97 dem_sblnum // cleaned later for all years
	    replace pcgc_foodsecuritystatus = . if pcgc_foodsecuritystatus==9
	    replace pcgc_foodsecuritystatus = 0 if pcgc_foodsecuritystatus==1
	    replace pcgc_foodsecuritystatus = 1 if inrange(pcgc_foodsecuritystatus, 2, 4)
	    label_indicator, var(pcgc_foodsecuritystatus)
	    rename pcgc_foodsecuritystatus pcgc_foodinsecure
	
	gen hasemtchiw = !mi(emtch_yearcompleted)
	gen haspstchiw = !mi(pstch_yearcompleted)
	gen hashbcpiw = !mi(hbcp_yearquestionnairecompleted)
	gen hasemadmiw = !mi(emadm_schoolcategory)
	gen haspsadmiw = !mi(psadm_locationofprogram)
	
	* some renaming to make consistent across years
	rename pcgc_feelangryw_child97 pcgc_feelangrywchild
	rename pcgc_estcurrentweight pcgc_estweightlastdocvisit
	rename pcgc_estimatedweight pcgc_estcurrentweight
	
	* making missing vars for variables that exist in other years but not in 1997
	foreach var in emtch_physicalability pcgc_estheightlastdocvisit pcgc_estcurrentheight ///
		pcgc_weightmeasured chld_goodjob chld_discouraged chld_worry chld_partofschool ///
		chld_closetoschool chld_happyatschool chld_safeatschool chld_skinproblems ///
		chld_schoolexpectations chld_degreeexpectations {
	    gen `var' = .
	    label variable `var' "NOT IN 1997"
	}
	* these variables are not included in other years, make a list of them for later
	ds emtch_suddenmoodswings-emtch_actsupinclass pstch_suddenmoodswings-pstch_makesexcessivedemands hbcp_suddenmoodswings-hbcp_worriestoomuch
	local mental_health_list "`r(varlist)'"

    } // end if 1997
	
	if `year'==2002 {
	 
	    gen haschiw = !mi(Q23IWYR)
	    gen haspcgciw = 1
	    gen haspcghiw = !mi(Q22J1)

	    drop CHREL HHREL CHLDREL OCGCREL OCGHREL EMSREL ASMREL Q21D18* Q21D2-Q21D2D_7 ///
	    Q21D3M-Q21D31D Q21F1-Q21H22F Q21H23A-Q21H32F Q23H6K Q25A14* 
	     
	    foreach var of varlist Q23K25A-Q23K25N {
	        local label: variable label `var'
	        local newlabel "NUM FRIENDS `label'"
		label variable `var' "`newlabel'"
	    }
	    label var Q23L6H "OTHER MONEY"
	    label var Q29B3C "NUM SPECIAL EDUCATION"
	    label var Q29D4_5 "SPECIAL ED CERTIFICATION"
	    
	    * renaming using variable labels which are more consistent across years:
	    foreach var of varlist Q21A2-Q24APW {
	        if inlist("`var'", "Q21A2", "Q24A1") local prefix "pcgc" // primary caregiver child file + assessments of height and weight
		if "`var'"=="Q22J1" local prefix "pcgh" // primary caregiver household file
		if inlist("`var'", "Q23E1", "Q24MONTH", "Q24B2") local prefix "chld" // child file + assessments excluding height and weight
		if "`var'"=="Q25A1B" local prefix "ocgc" // other caregiver child file
		if "`var'"=="Q26B1" local prefix "ocgh" // other caregiver household file
		if "`var'"=="Q29A1MON" local prefix "emtch" // elem or middle school teacher file
		
		di "VAR NUMBER `var'"
		switch_varname_and_varlabel, var(`var') prefix(`prefix')
 
	    }
	    rename ER33603 reltohead
	    foreach var of varlist reltohead-PCGEMS02{
	        rename `var' dem_`var'
	        rename dem_`var', lower
	    }
	    * set up some demographic variables
	    gen dem_childofheads = inlist(dem_reltohead, 30, 33, 35, 38, 83)
	    label var dem_childofheads "Based on ER33603"
	    rename dem_sblnum03 dem_sblnum // cleaned later for all years
	    drop dem_sblnum01
	    replace dem_sblnum = . if dem_sblnum==10
	    replace dem_sblnum = 10 if dem_sblnum==9
	    
	    gen hasemtchiw = !mi(emtch_interviewyear)
	    gen haspstchiw = 0
	    gen hashbcpiw = 0
	    gen hasemadmiw = 0 
	    gen haspsadmiw = 0 
	
	    * some renaming to make consistent across years
	    rename pcgc_childageattimeofpcgiwyears dem_ageatpcg
	    rename (pcgc_monthpcginterviewcompleted pcgc_yearpcginterviewcompleted) ///
		    (dem_pcgiwmon dem_pcgiwyr)
	    rename (chld_childinterviewmonth chld_childinterviewyear emtch_interviewmonth ///
		    emtch_interviewyear) (dem_chiwmon dem_chiwyr emtch_monthcompleted emtch_yearcompleted)
	    rename chld_langofadmin chld_spanishadministration
	    replace chld_spanishadministration = chld_spanishadministration-1
	    rename (*stdscore) (*stndscore)
	    rename emtch_communicatesstories emtch_commnctsstories_peers
	    rename (emtch_reading_writinginstruction emtch_mathinstruction) ///
		   (emtch_childinread_writeinstrctns emtch_childinmathinstrctnsegment)
	    gen dem_homeschl=pcgc_privorpublsch==3 if inrange(pcgc_privorpublsch, 1, 3)
	    rename chld_gradeinsch chld_whatgradeinschool
	    rename (emtch_studentsinclass emtch_fulltimeteachers) ///
		   (emtch_numstudentsinclass emtch_numfulltimeteachers)
	    rename (emtch_white emtch_black emtch_hispanic emtch_asian emtch_americanindian ///
		    emtch_otherethnicity) (emtch_numwhitestudents emtch_numblackstudents ///
		    emtch_numhispanicstudents emtch_numasianstudents emtch_numamericanindianstudents ///
		    emtch_numotherethnicitystudents)
	    rename (emtch_standardizedtest emtch_teachertest emtch_portfolios) ///
		   (emtch_state_localstandardizedtes emtch_teachermadetests emtch_performancebasedassessment)
	    rename pcgc_pcgratedchildhealth pcgc_pcgratedchildhlth
	    gen pcgc_fathersresidence = 1 if pcgc_livew_biodad==1
	    foreach var in pcgc_haveadoptivedad pcgc_havestepdad pcgc_haveothdadfigure pcgc_havelegalguardiandad {
	        replace pcgc_fathersresidence = 1 if `var'==1
	    }
	    replace pcgc_fathersresidence = 0 if inlist(pcgc_haveadoptivedad, 2, 5) ///
		   & inlist(pcgc_havestepdad, 2, 5) &  inlist(pcgc_haveothdadfigure, 2, 5) ///
		   & inlist(pcgc_havelegalguardiandad, 2, 5)
	    rename pcgc_child_prcaregivertogether pcgc_childpcgtogether
	    rename (pcgc_rulesonamtoftv pcgc_limitsonkindsoftv pcgc_permittvduringmeals ///
		    pcgc_rulesonbedtime pcgc_setlimitsonsweets pcgc_rulesonw_whomchildinteracts ///
		    pcgc_rulesonafterschactivities pcgc_rulesabouthomewk pcgc_discussrulesw_child) ///
		    (pcgh_limitsontv pcgh_limitsonkindsoftv pcgh_notvduringmeals ///
		    pcgh_limitsonbedtime pcgh_limitsonsweets pcgh_trytocontrolwhochildiswith ///
		    pcgh_controlchilddoesafterschl pcgh_sethomeworktime pcgh_discussruleswithchild)
	    rename pcgh_preschchildsufferifmomemplo pcgh_preschchildsuffersifmomempl
	    rename (pcgc_pearlinselfefficacyscale pcgc_agginparentingscale pcgc_distressscale) ///
		   (pcgc_pearlinselfefficacyscalesho pcgc_agginparentingscaleshort pcgc_distressscalek6)
	    rename (pcgc_childbothersme pcgc_givingupmoreoflife pcgc_feelangry) ///
		   (pcgc_bothersome pcgc_givinguplife pcgc_feelangrywchild)
	    rename pcgh_sosadcouldntbecheer pcgh_sosadcouldntbecheered
	
	#delimit ;
	foreach var in pstch_monthcompleted hbcp_monthquestionnairecompleted pstch_yearcompleted
		hbcp_yearquestionnairecompleted dem_chrace dem_immigfam dem_immigchild pcgc_foodinsecure
		chld_calrawscore chld_calstndscore chld_broadmathscore chld_broadmathpercentilerank
		chld_broadmathwscore chld_calwscore chld_calpercentilerank chld_likebeingmyselfe
		chld_doimportantthings pcgc_pcrawscore emtch_howlongstudent
		emtch_commnctsstories_teachrs emtch_instructspeers emtch_goodlistener
		`mental_health_list' emtch_academiccompetence emtch_socialcompetence
		emtch_physicalcompetence pstch_academiccompetenceofchild
		pstch_socialcompetenceofchild pstch_physicalcompetenceofchild emtch_skippedgrades
		emtch_repeatedgrades pcgc_numberoffriends pcgc_childsage pcgc_kindergarten 
		pcgc_firstgrade pcgc_notinprogram emtch_currentgrade pstch_childreninchildsclass
		pstch_fulltimeteachers pstch_parttimeteachers pstch_numberofwhitechildren
		pstch_numberofblackchildren pstch_numberofhispanicchildren pstch_numberofasianchildren
		pstch_numberofindianchildren pstch_numberother pstch_daycompleted pstch_male_female
		pstch_ethnicity pstch_highestlevelofeducation pstch_yearborn emadm_schoolcategory
		emadm_kindergarten emadm_firstgrade emadm_secondgrade emadm_thirdgrade
		emadm_fourthgrade emadm_fifthgrade emadm_sixthgrade emadm_seventhgrade
		emadm_eighthgrade emadm_ninthgrade emadm_tenthgrade emadm_eleventhgrade
		emadm_twelfthgrade emadm_currentstudentenrollment emadm_numberwhitestudents
		emadm_numberblackstudents emadm_numberhispanicstudents emadm_numberasianstudents
		emadm_numberindianstudents emadm_numberotherstudents emadm_numbfreelunchprog
		emadm_numbsubsidizedlunchprog emadm_breakfastprovisions emadm_studentratio
		emadm_dollarsspent_student psadm_numberofchildren psadm_numberchildrenwhite_nonhis
		psadm_numberchildrenblack_nonhis psadm_numberchildrenhispanic psadm_numberchildrenasian
		psadm_numberchildrenamericanindi psadm_numberchildrenother psadm_studentratio
		psadm_dollarsspentperstudent pcgc_neonatalintnsvcare pcgc_medicaidpaidbillspregnant 
		pcgc_wicprogrampregnant pcgc_foodstampspregnant pcgc_freefoodpregnant
		pcgc_adcafdcpregnant pcgc_otherhelppregnant pcgc_learningdisability
		pcgh_livew_spouseorpartner pcgc_weekdaytv pcgc_weekendtv pcgc_readtochild 
		pcgc_shownphysicalaffection pcgc_jokedorplayed pcgh_receivedhelp pcgh_providedhelp
		pcgh_receiveemotionalsupport pcgh_providedemotionalsupport pcgh_amountofrules
		pcgh_rulesstrictlyenforced pcgh_notquestionteachingmethods
		pcgh_kidsshouldallbetreatedsame pcgh_kidsshouldalwaysobeyteacher
		pcgh_kidstreatedasfriendsbyadult pcgh_naturalabilitysuccessinsch
		pcgh_hardworksuccessinsch pcgh_beinglikedbyothsuccessinsch pcgh_decisionsmadebymanofhh
		pcgh_workiseithermensorwomens pcgh_candoanything pcgh_futuredependsonme
		pcgh_candolittletochangelife pcgh_tiredfornoreason pcgh_sonervouscantcalm
		pcgh_sorestlesscantsitstill pcgh_depressed pcgc_hardertocare pcgc_dobetterw_outchild
		pcgh_familymembersinneighborhood pcgh_goodfriendsinneighborhood
		pcgh_childrenyouknowbyname pcgh_proportionownhouse pcgh_proportionhispanic_latino
		pcgh_proportionasian pcgh_proportionwhite pcgh_proportionafricanamerican
		pcgh_amountattendreligiousservic pcgh_healthinsuremployer pcgh_healthinsurprivateplan 
		pcgh_healthinsurmedicare pcgh_healthinsuranothersource pcgh_healthinsurnotatall
		pcgh_moneyleftatendofmonth pcgh_actvtsouthome_working pcgh_actvtsouthome_school_colleg
		pcgh_actvtsouthome_intrainingpro pcgh_actvtsouthome_lookingforwor 
		pcgh_actvtsouthome_volunteerwork pcgh_actvtsouthome_recreatnlacti 
		pcgh_actvtsouthome_other {;
	    gen `var'=.;
	    label variable `var' "NOT IN 2002";
	} ;
	#delimit cr
	
        } // end if 2002

	if `year'==2007 {
	
	    gen haschiw = !mi(Q33IWYR)
	    gen haspcgciw = !mi(Q31IWYR)
	    gen haspcghiw = !mi(Q31IWYR)
	     
	    drop DEMREL07 PCHREL07 PHHREL07 CHLREL07 OCHREL07 OHHREL07 ASMREL07 ///
	    Q31D2C-Q31E4A Q31H15* Q31H16* Q31H17* Q31H32-Q31H32H Q33L3-Q33L3H ///
	    Q35A141-Q35A143
	    
	    foreach var of varlist Q33K25A-Q33K25P {
	        local label: variable label `var'
	        local newlabel "NUM FRIENDS `label'"
		label variable `var' "`newlabel'"
	    }
	    
	    * renaming using variable labels since they are more consistent across years:
	    foreach var of varlist Q31A0_LB-WGTZ07 {
	        if inlist("`var'", "Q31A0_LB", "Q34A1") local prefix "pcgc" // primary caregiver child file + assessments of height and weight
		if "`var'"=="Q32J1" local prefix "pcgh" // primary caregiver household file
		if inlist("`var'", "Q33E1", "Q34MONTH", "Q34B3") local prefix "chld" // child file + assessments excluding height and weight
		if "`var'"=="Q35A1A" local prefix "ocgc" // other caregiver child file
		if "`var'"=="Q36B1" local prefix "ocgh" // other caregiver household file
		
		di "VAR NUMBER `var'"
		switch_varname_and_varlabel, var(`var') prefix(`prefix')
	    }
	    
	    rename ER33903 reltohead
	    foreach var of varlist reltohead-BIOGPR07{
	        rename `var' dem_`var'
	        rename dem_`var', lower
	    }
	    
	    * prep some demographic vars
	    gen dem_childofheads = inlist(dem_reltohead, 30, 33, 35, 38, 83)
	    label var dem_childofheads "Based on ER33903"
	    rename dem_sblnum07 dem_sblnum // cleaned later for all years
	    replace dem_sblnum = 10 if dem_sblnum==9
	    
	    gen hasemtchiw = 0
	    gen haspstchiw = 0
	    gen hashbcpiw = 0
	    gen hasemadmiw = 0 
	    gen haspsadmiw = 0 
	
	    * some renaming to make consistent across years
	    rename pcgc_childageattimeofpcgiwyears dem_ageatpcg
	    rename (pcgc_yearpcginterviewcompleted pcgc_monthpcginterviewcompleted) ///
		   (dem_pcgiwyr dem_pcgiwmon)
	    rename (chld_childinterviewyear chld_childinterviewmonth) (dem_chiwyr dem_chiwmon)
	    rename chld_langofadmin chld_spanishadministration
	    replace chld_spanishadministration = 1-chld_spanishadministration
	    rename (chld_lwstdscore chld_appliedprobstdscore) (chld_lwstndscore chld_appliedprobstndscore)
	    rename chld_broadreadingstndscore chld_broadreadingscore
	    rename chld_mathcomparedtoothskills chld_mathcomparedt0othskills
	    rename chld_readingcomparedtoothskills chld_readingcomparedt0othskills
	    rename pcgc_expectededucation pcgc_expectededuc
	    gen dem_homeschl=pcgc_privateorpublicsch==3 if inrange(pcgc_privateorpublicsch, 1, 3)
	    replace pcgc_gradeinschool = . if inlist(pcgc_gradeinschool, 0, 13)
	    rename pcgc_gradeinschool pcgc_gradeinsch
	    rename pcgc_innatbreakfastprog pcgc_infedbreakfastprog
	    rename pcgc_innatlunchprog pcgc_infedlunchprog
	    rename pcgc_everattendprivateschool pcgc_everattendprivschool
	    rename (pcgc_everinspecialeduc pcgc_everingiftedprogram) ///
		   (pcgc_everinspeceduc pcgc_everingiftedprog)
	    rename chld_gradeinsch chld_whatgradeinschool
	    rename pcgc_privateorpublicschool pcgc_privorpublsch
	    rename (pcgc_orthopedicimpairment pcgc_developmentaldelay pcgc_otherchroniccond)  /// 
		   (pcgc_orthopedimpairmt pcgc_developmentdelay pcgc_othchroniccond)
	    rename pcgc_drvisitemotionalprobs pcgc_drvisitemotprobs
	    rename pcgc_pcgratedchildhealth pcgc_pcgratedchildhlth
	    gen pcgc_fathersresidence = 1 if pcgc_livew_biodad==1
	    foreach var in pcgc_haveadoptivedad pcgc_havestepdad pcgc_haveothdadfigure pcgc_havelegalguardiandad {
	        replace pcgc_fathersresidence = 1 if `var'==1
	    }
	    replace pcgc_fathersresidence = 0 if inlist(pcgc_haveadoptivedad, 2, 5) ///
	            &  inlist(pcgc_havestepdad, 2, 5) &  inlist(pcgc_haveothdadfigure, 2, 5) ///
		    & inlist(pcgc_havelegalguardiandad, 2, 5)
	    rename pcgc_child_prcaregivertogether pcgc_childpcgtogether
	    rename (pcgc_rulesonamtoftv pcgc_limitsonkindsoftv pcgc_permittvduringmeals ///
		    pcgc_rulesonbedtime pcgc_setlimitsonsweets pcgc_rulesonwhomchildinteracts ///
		    pcgc_rulesonafterschoolactivitie pcgc_rulesabouthomework pcgc_discussrulesw_child) ///
		   (pcgh_limitsontv pcgh_limitsonkindsoftv pcgh_notvduringmeals ///
		    pcgh_limitsonbedtime pcgh_limitsonsweets pcgh_trytocontrolwhochildiswith ///
		    pcgh_controlchilddoesafterschl pcgh_sethomeworktime  ///
		    pcgh_discussruleswithchild)
	    rename (*importantquality) (*importquality)
	    rename pcgh_preschchildsufferifmomemplo pcgh_preschchildsuffersifmomempl
	    rename pcgh_dadhoodmostfufillexper pcgh_dadhoodmostfulfilexper
	    rename pcgh_momhoodmostfufillexper pcgh_momhoodmostfulfilexper
	    rename pcgh_dadshouldinteractw_child pcgh_dadsshouldinteractw_child
	    rename pcgh_cantsolveproblems pcgh_cantsolveprobs
	    rename pcgh_sosadcouldntbecheer pcgh_sosadcouldntbecheered
	    rename pcgh_beingaparentishard pcgh_beingparentishard
	    replace pcgh_importanceofreligion = 4 if pcgh_importanceofreligion==3
	    replace pcgh_importanceofreligion = 3 if pcgh_importanceofreligion==1
	    replace pcgh_importanceofreligion = 1 if pcgh_importanceofreligion==4
	    rename (pcgc_pearlinselfefficacyscale pcgc_agginparentingscale pcgc_distressscale) ///
		   (pcgc_pearlinselfefficacyscalesho pcgc_agginparentingscaleshort pcgc_distressscalek6)
	    rename (pcgc_childbothersme pcgc_givingupmoreoflife pcgc_feelangry) ///
		   (pcgc_bothersome pcgc_givinguplife pcgc_feelangrywchild)
	    
	    /* in 2007 they switched to only having the pcgh file apply to one child 
	     in the HH and coding as 0 for the other child. This chunk of code applies 
	     the answer to all children in the household*/
	    foreach var of varlist pcgh_*importquality pcgh_husb_wifesharetasks pcgh_womenhappierathome ///
		    pcgh_betterifmanearnsliving pcgh_betterwifehelphusbcareer pcgh_employmomequalunemploymom ///
		    pcgh_independindaughtersandsons pcgh_preschchildsuffersifmomempl pcgh_dadhoodmostfulfilexper ///
		    pcgh_momhoodmostfulfilexper pcgh_dadsshouldinteractw_child pcgh_dadasinvolvedasmom ///
		    pcgh_dadandmomequal pcgh_familyfightsalot pcgh_familycriticizes pcgh_personofworth ///
		    pcgh_goodqualities pcgh_feellikeafailure pcgh_dothingswell pcgh_notmuchtobeproudof ///
		    pcgh_positiveattitude pcgh_satisfiedwithself pcgh_wantmorerespect pcgh_feeluselessattimes ///
		    pcgh_thinkimnogood pcgh_cantsolveprobs pcgh_pushedaround pcgh_littlecontrol pcgh_feelhelpless ///
		    pcgh_nervous pcgh_hopeless pcgh_restless  pcgh_everythinganeffort pcgh_sosadcouldntbecheered ///
		    pcgh_worthless pcgh_beingparentishard pcgh_feeltrappedasparent pcgh_childrenarework ///
		    pcgh_feeltiredraisingfamily pcgh_neighborhoodrating pcgh_importanceofreligion { 
	       sort cds_int_num_`year' `var'
	        replace `var' = `var'[_n+1] if `var'==0 & cds_int_num_`year'==cds_int_num_`year'[_n+1]
	    }
	
	* generate missing variables for the questions asked in other years but not in 2007
	#delimit ;
	    foreach var in emtch_monthcompleted pstch_monthcompleted hbcp_monthquestionnairecompleted 
		    emtch_yearcompleted pstch_yearcompleted hbcp_yearquestionnairecompleted dem_chrace
		    chld_calrawscore chld_calstndscore chld_calpercentilerank chld_calwscore chld_broadmathscore
		    chld_broadmathpercentilerank chld_broadmathwscore chld_likebeingmyselfe chld_doimportantthings
		    pcgc_pcrawscore `mental_health_list' emtch_howlongstudent emtch_commnctsstories_peers  
		    emtch_commnctsstories_teachrs emtch_recallsexperiences emtch_instructspeers  
		    emtch_respondstoquestions emtch_rephrasesquestions emtch_easilyunderstood emtch_goodlistener
		    emtch_academiccompetence emtch_socialcompetence emtch_physicalcompetence pstch_academiccompetenceofchild
		    pstch_socialcompetenceofchild pstch_physicalcompetenceofchild emtch_schoolexpectations 
		    emtch_specialedbystateguidelines emtch_languageminority emtch_limitedenglishproficient 
		    emtch_skippedgrades emtch_gifted_talentedprogram emtch_repeatedgrades emtch_childinread_writeinstrctns
		    emtch_childinmathinstrctnsegment emtch_readingability emtch_mathability emtch_physicalability 
		    emtch_differentteachers pcgc_numberoffriends pcgc_childsage pcgc_earlyinterventprogs 
		    pcgc_agebegankgartenyrs pcgc_chldinpresch pcgc_kindergarten pcgc_firstgrade pcgc_notinprogram
		    emtch_currentgrade pstch_childreninchildsclass pstch_fulltimeteachers pstch_parttimeteachers
		    pstch_numberofwhitechildren pstch_numberofblackchildren pstch_numberofhispanicchildren 
		    pstch_numberofasianchildren pstch_numberofindianchildren pstch_numberother emtch_numstudentsinclass
		    emtch_numfulltimeteachers emtch_numwhitestudents emtch_numblackstudents emtch_numhispanicstudents
		    emtch_numasianstudents emtch_numamericanindianstudents  emtch_numotherethnicitystudents 
		    pstch_daycompleted emtch_male_female pstch_male_female emtch_ethnicity pstch_ethnicity 
		    emtch_monthsatschool emtch_yearsatschool emtch_teachereducation pstch_highestlevelofeducation
		    emtch_yearborn pstch_yearborn emtch_state_localstandardizedtes emtch_teachermadetests 
		    emtch_performancebasedassessment emadm_schoolcategory emadm_kindergarten emadm_firstgrade
		    emadm_secondgrade emadm_thirdgrade emadm_fourthgrade emadm_fifthgrade emadm_sixthgrade emadm_seventhgrade
		    emadm_eigthgrade emadm_ninthgrade emadm_tenthgrade emadm_eleventhgrade emadm_twelfthgrade 
		    emadm_currentstudentenrollment emadm_numberwhitestudents emadm_numberblackstudents 
		    emadm_numberhispanicstudents emadm_numberasianstudents emadm_numberindianstudents 
		    emadm_numberotherstudents emadm_numbfreelunchprog emadm_numbsubsidizedlunchprog emadm_breakfastprovisions
		    emadm_studentratio emadm_dollarsspent_student psadm_numberofchildren psadm_numberchildrenwhite_nonhis
		    psadm_numberchildrenblack_nonhis psadm_numberchildrenhispanic psadm_numberchildrenasian 
		    psadm_numberchildrenamericanindi psadm_numberchildrenother psadm_studentratio psadm_dollarsspentperstudent 
		    pcgc_neonatalintnsvcare pcgc_brfeedasinfant pcgc_medicaidpaidbillspregnant pcgc_wicprogrampregnant 
		    pcgc_foodstampspregnant pcgc_freefoodpregnant pcgc_adcafdcpregnant pcgc_otherhelppregnant pcgc_learningdisability
		    pcgh_livew_spouseorpartner pcgc_weekdaytv pcgc_weekendtv pcgc_readtochild pcgc_shownphysicalaffection
		    pcgc_jokedorplayed pcgh_receivedhelp pcgh_providedhelp pcgh_receiveemotionalsupport pcgh_providedemotionalsupport
		    pcgh_amountofrules pcgh_rulesstrictlyenforced pcgh_notquestionteachingmethods pcgh_kidsshouldallbetreatedsame
		    pcgh_kidsshouldalwaysobeyteacher pcgh_kidstreatedasfriendsbyadult pcgh_naturalabilitysuccessinsch 
		    pcgh_hardworksuccessinsch pcgh_beinglikedbyothsuccessinsch pcgh_decisionsmadebymanofhh 
		    pcgh_workiseithermensorwomens pcgh_futuredependsonme pcgh_candolittletochangelife 
		    pcgh_candoanything pcgh_tiredfornoreason pcgh_sonervouscantcalm pcgh_sorestlesscantsitstill
		    pcgh_depressed pcgc_hardertocare pcgc_dobetterw_outchild pcgh_familymembersinneighborhood 
		    pcgh_goodfriendsinneighborhood pcgh_childrenyouknowbyname pcgh_proportionownhouse 
		    pcgh_proportionhispanic_latino pcgh_proportionasian pcgh_proportionwhite pcgh_proportionafricanamerican 
		    pcgh_amountattendreligiousservic pcgh_healthinsuremployer pcgh_healthinsurprivateplan pcgh_healthinsurmedicare 
		    pcgh_healthinsuranothersource pcgh_healthinsurnotatall pcgh_moneyleftatendofmonth pcgh_actvtsouthome_working 
		    pcgh_actvtsouthome_school_colleg  pcgh_actvtsouthome_intrainingpro pcgh_actvtsouthome_lookingforwor
		    pcgh_actvtsouthome_volunteerwork pcgh_actvtsouthome_recreatnlacti pcgh_actvtsouthome_other 
		    dem_immigchild dem_immigfam pcgc_foodinsecure {;
	        gen `var'=.;
	        label variable `var' "NOT IN 2007";
	    };
	    #delimit cr
        } // end if 2007

	* cleaning in all years
	
	*************
	* age/dates *
	*************

	replace dem_ageatpcg = . if dem_ageatpcg==float(999.9)
	gen moyr_pcgiw = ym(dem_pcgiwyr, dem_pcgiwmon)
	gen dem_birthmoyr = moyr_pcgiw - dem_ageatpcg
	
	foreach month in dem_chiwmon emtch_monthcompleted pstch_monthcompleted hbcp_monthquestionnairecompleted  {
	    replace `month' = . if inlist(`month', 0, 99)
	}
	foreach yr in dem_chiwyr emtch_yearcompleted pstch_yearcompleted hbcp_yearquestionnairecompleted {
	    replace `yr' = . if inlist(`yr', 0, 9999)
	}
	
	gen moyr_chiw = ym(dem_chiwyr, dem_chiwmon)
	gen moyr_emtchiw = ym(emtch_yearcompleted, emtch_monthcompleted)
	gen moyr_pstchiw = ym(pstch_yearcompleted, pstch_monthcompleted)
	gen moyr_hbcpiw = ym(hbcp_yearquestionnairecompleted, hbcp_monthquestionnairecompleted)

	********
	* race *
	********
	 replace dem_chrace = . if dem_chrace==9
	 
	************
	* siblings *
	************
	replace dem_sblnum = 0 if dem_sblnum>=10
	label variable dem_sblnum "Number of siblings living in FU, based on SBLNUM"
		
	********************************* 
	* ability vars from assessments *
	*********************************
	* test adminstration
	rename chld_spanishadministration chld_spanishassessment 

	* scores
	foreach var in chld_lwrawscore chld_pcrawscore chld_calrawscore ///
	chld_appliedprobrawscore chld_digitspanforwardrawscore  ///
	chld_digitspanbackwardrawscore chld_digitspantotalrawscore {
	    replace `var' = . if `var'==99
	}
	foreach var in chld_lwstndscore chld_pcstndscore chld_broadreadingscore ///
		chld_calstndscore chld_appliedprobstndscore chld_broadmathscore ///
		chld_broadreadingpercentilerank chld_broadreadingwscore ///
		chld_broadmathpercentilerank chld_broadmathwscore ///
		chld_lwwscore chld_pcwscore chld_calwscore chld_appliedprobwscore ///
		chld_lwpercentilerank chld_pcpercentilerank chld_calpercentilerank ///
		chld_appliedprobpercentilerank {
	    replace `var' = . if `var'==999
	}
	
	foreach var in chld_lwrawscore chld_pcrawscore chld_calrawscore ///
		chld_appliedprobrawscore chld_digitspanforwardrawscore ///
		chld_digitspanbackwardrawscore chld_digitspantotalrawscore ///
		chld_lwstndscore chld_pcstndscore chld_calstndscore ///
		chld_appliedprobstndscore chld_lwwscore chld_pcwscore ///
		chld_calwscore chld_appliedprobwscore chld_lwpercentilerank ///
		chld_pcpercentilerank chld_calpercentilerank ///
		chld_appliedprobpercentilerank {

	    local newname = subinstr("`var'", "_lw", "_read_lw", .)
	    local newname = subinstr("`newname'", "_pc", "_read_pc", .)
	    local newname = subinstr("`newname'", "_cal", "_math_cal", .)
	    local newname = subinstr("`newname'", "_appliedprob", "_math_ap", .)	
	
	    local newname = subinstr("`newname'", "rawscore", "_raw", .)
	    local newname = subinstr("`newname'", "stndscore", "_ss", .)
	    local newname = subinstr("`newname'", "wscore", "_ws", .)
	    local newname = subinstr("`newname'", "percentilerank", "_pctile", .)
	    rename `var' `newname'
	    
	}
	rename chld_broadreadingscore chld_read_total_ss
	rename chld_broadmathscore chld_math_total_ss
	rename chld_broadreadingwscore chld_read_total_ws
	rename chld_broadmathwscore chld_math_total_ws
	rename chld_broadreadingpercentilerank chld_read_total_pctile
	rename chld_broadmathpercentilerank chld_math_total_pctile
	* raw is raw count of correct questions
	* ss is standardized score
	* ws is W score (see woodcock johnson documentation) 
	* we are using percentile rank
	
	******************************
	* child self confidence vars *
	******************************
	* note: only asked if child is 8 or older
	
	foreach var in chld_mathskillgenrate chld_mathskillincontextpeers ///
		chld_mathcomparedt0othskills chld_achieveinmaththisyr ///
		chld_learningsomethingnewinmath chld_howhardismath ///
		chld_howusefulismath chld_importanceofmath chld_interestinmath ///
		chld_howmuchlikemath chld_readingskillgenrate chld_readingskillincontextpeers ///
		chld_readingcomparedt0othskills chld_achieveinreadingthisyr ///
		chld_learningsomethingnewinreadi chld_howhardisreading chld_howusefulisreading ///
		chld_importanceofreading chld_interestinreading chld_howmuchlikereading {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	}
	rename (chld_mathskillgenrate chld_mathskillincontextpeers ///
		chld_mathcomparedt0othskills chld_achieveinmaththisyr ///
		chld_learningsomethingnewinmath chld_howhardismath chld_howusefulismath ///
		chld_importanceofmath chld_interestinmath chld_howmuchlikemath) ///
		(chld_mathcnf_rateownskill chld_mathcnf_skillreltopeers ///
		chld_mathcnf_skillrelothersubj chld_mathcnf_expectedperf ///
		chld_mathcnf_goodatlearnnew chld_mathcnf_howhard ///
		chld_mathcnf_howuseful chld_mathcnf_imptobegood ///
		chld_mathcnf_interesting chld_mathcnf_enjoy)
	rename (chld_readingskillgenrate chld_readingskillincontextpeers ///
		chld_readingcomparedt0othskills chld_achieveinreadingthisyr ///
		chld_learningsomethingnewinreadi chld_howhardisreading chld_howusefulisreading ///
		chld_importanceofreading chld_interestinreading chld_howmuchlikereading) ///
		(chld_readcnf_rateownskill chld_readcnf_skillreltopeers ///
		chld_readcnf_skillrelothersubj chld_readcnf_expectedperf ///
		chld_readcnf_goodatlearnnew chld_readcnf_howhard ///
		chld_readcnf_howuseful chld_readcnf_imptobegood ///
		chld_readcnf_interesting chld_readcnf_enjoy)
		
	rename chld_likebeingmyselfe chld_likebeingmyself
	rename chld_proud chld_lottobeproudof
	rename chld_dothingsaswellasothers chld_dothingsaswellasoth
	foreach var in chld_doimportantthings chld_likebeingmyself chld_lottobeproudof ///
		chld_dothingsaswellasoth chld_goodthingsaboutme chld_goodasothers ///
		chld_othersthinkiamgood chld_dothingswell {
		replace `var' = . if inlist(`var', 0, 8, 9)
		local newname = subinstr("`var'", "chld_", "chld_gencnf_", 1)
		rename `var' `newname'
		}
	foreach var in chld_abilityselfconceptsmath chld_abilityselfconceptsread chld_globalselfconcept {
	    replace `var' = . if !inrange(`var', 1, 7)
	}
	rename (chld_abilityselfconceptsmath chld_abilityselfconceptsread chld_globalselfconcept) ///
		(chld_mathcnf_selfconcept chld_readcnf_selfconcept chld_gencnf_selfconcept)
		
	* chld_mathcnf_rateownskill-chld_mathcnf_enjoy are averaged for chld_mathcnf_selfconcept
	* chld_readcnf_rateownskill-chld_readcnf_enjoy are averaged for chld_readcnf_selfconcept
	* chld_gencnf_doimportantthings-chld_gencnf_dothingswell are averaged for chld_gencnf_selfconcept
	
	foreach var in chld_goodjob chld_discouraged chld_worry chld_partofschool ///
		chld_closetoschool chld_happyatschool chld_safeatschool chld_skinproblems {
	   replace `var' = . if inlist(`var', 8, 9)
	}
	rename (chld_goodjob chld_discouraged chld_worry chld_partofschool chld_closetoschool ///
	chld_happyatschool chld_safeatschool chld_skinproblems) (chld_worrynogoodjob ///
	chld_discouragedaboutfuture chld_worryaboutmoney chld_feelpartofschool chld_feelclosetopeers ///
	chld_happyatschool chld_safeatschool chld_skinproblems)
	
	// note only for those over 12
	gen chld_expectededuc = .
	replace chld_expectededuc = 1 if chld_schoolexpectations==1
	replace chld_expectededuc = 2 if chld_schoolexpectations==2
	replace chld_expectededuc = 3 if chld_schoolexpectations==4
	replace chld_expectededuc = 4 if chld_schoolexpectations==5
	replace chld_expectededuc = 5 if chld_schoolexpectations==3
	replace chld_expectededuc = 6 if chld_schoolexpectations==6
	replace chld_expectededuc = 7 if chld_schoolexpectations==7 & chld_degreeexpectations==1
	replace chld_expectededuc = 8 if chld_schoolexpectations==7 & inrange(chld_degreeexpectations, 2, 5)
	label values chld_expectededuc Q21B2L
	local label: variable label chld_schoolexpectations
	local label2: variable label chld_degreeexpectations
	label variable chld_expectededuc "`label' and `label2'"	
	
	********************************
	* parent passage comprehension *
	********************************
	replace pcgc_pcrawscore = . if pcgc_pcrawscore>43
	
	**********************************
	* teacher opinions about ability *
	**********************************
	replace emtch_howlongstudent = . if inlist(emtch_howlongstudent, 8, 9)
	rename emtch_howlongstudent emtch_lengthknowingstudent
	rename (emtch_commnctsstories_peers  emtch_commnctsstories_teachrs) ///
		(emtch_communicatestorypeers emtch_communicatestorytchrs)
	foreach var in emtch_recallsexperiences emtch_communicatestorypeers emtch_instructspeers ///
		emtch_communicatestorytchrs emtch_respondstoquestions emtch_rephrasesquestions ///
		emtch_easilyunderstood emtch_goodlistener {
		replace `var' = . if inlist(`var', 8, 9)
		local newname = subinstr("`var'", "emtch_", "emtch_gen_", 1)
		rename `var' `newname'
		}
	foreach var of varlist emtch_suddenmoodswings-emtch_actsupinclass ///
		pstch_suddenmoodswings-pstch_makesexcessivedemands hbcp_suddenmoodswings-hbcp_worriestoomuch {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	    replace `var' = 0 if `var'==3
	    replace `var' = 1 if inlist(`var', 1, 2)
	}
	rename (hbcp_feelsnooneloveshim_her hbcp_cheatsortellslies hbcp_fearfuloranxious ///
		hbcp_bulliesoriscruel hbcp_restlessoroveractive hbcp_actswithoutthinking ///
		hbcp_feelsworthlessorinferior hbcp_notlikedbyotherchildren hbcp_verystrongtemper ///
		hbcp_unhappy_sadordepressed  hbcp_doesntgetinvolvedw_others ///
		hbcp_breaksthingsonpurpose  hbcp_demandsalotofattention ///
		hbcp_hangsaroundwithtrouble hbcp_toodependentonothers) ///
		(hbcp_feelsnolove hbcp_cheats_tellslies hbcp_fearful_anxious ///
		hbcp_bullies_orismean hbcp_restless_overactive hbcp_actsw_outthinking ///
		hbcp_feelsinferior hbcp_notlikedbyothers hbcp_strongtemper ///
		hbcp_unhappy_sad  hbcp_withdrawn hbcp_breaksthings ///
		hbcp_demandsattention hbcp_hangsaroundw_trouble hbcp_dependentonothers) 
	gen hbcp_disobedientatschool = .
	gen hbcp_makesexcessivedemands = .
	foreach var of varlist pstch_suddenmoodswings-pstch_makesexcessivedemands {
	    local sub = subinstr("`var'", "pstch_","", 1)
	    gen tch_`sub' = emtch_`sub'
	    replace tch_`sub' = pstch_`sub' if mi(tch_`sub')
	    replace tch_`sub' = hbcp_`sub' if mi(tch_`sub')
	    local label: variable label emtch_`sub'
	    local label2: variable label pstch_`sub'
	    local label3: variable label hbcp_`sub'
	    label var tch_`sub' "`label' and `label2' and `label3'"
	    label_indicator, var(tch_`sub')
	    label_indicator, var(emtch_`sub')
	    label_indicator, var(pstch_`sub')	
	    label_indicator, var(hbcp_`sub')	
	}
	 
	foreach prefix in emtch pstch hbcp tch { 
	    rename (`prefix'_cheats_tellslies `prefix'_fearful_anxious `prefix'_bullies_orismean ///
		`prefix'_disobedientatschool `prefix'_doesntfeelsorry `prefix'_actsw_outthinking ///
		`prefix'_feelsinferior `prefix'_notlikedbyothers `prefix'_restless_overactive ///
		`prefix'_stubbornorirritable `prefix'_strongtemper ///
		`prefix'_unhappy_sad `prefix'_breaksthings `prefix'_dependentonothers ///
		`prefix'_feelsparanoid `prefix'_hangsaroundw_trouble `prefix'_makesexcessivedemands) ///
		(`prefix'_cheats `prefix'_fearful `prefix'_meantoothers `prefix'_disobedient ///
		`prefix'_feelsnoregret `prefix'_impulsive `prefix'_feelsworthless `prefix'_notliked ///
		`prefix'_restless `prefix'_stubborn `prefix'_strongtempered `prefix'_unhappy ///
		`prefix'_destructive `prefix'_dependent `prefix'_paranoid ///
		`prefix'_hangsaroundtrouble `prefix'_excessivedemandoftchr) // to match corresponding pcgc questions/opinions
	}
	drop hbcp_disobedient hbcp_excessivedemandoftchr
	 
	foreach var in emtch_academiccompetence emtch_socialcompetence emtch_physicalcompetence ///
		pstch_academiccompetenceofchild pstch_socialcompetenceofchild pstch_physicalcompetenceofchild {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	} 
	foreach domain in academic social physical {
	   gen tch_`domain'competence = emtch_`domain'competence
	   replace tch_`domain'competence = pstch_`domain'competenceofchild if mi(tch_`domain'competence)
	   local label: variable label emtch_`domain'competence
	   local label2: variable label pstch_`domain'competence
	   label var tch_`domain'competence "`label' and `label2''"
	}
	
	replace emtch_schoolexpectations = . if emtch_schoolexpectations>8
	rename emtch_schoolexpectations emtch_expectededuc
	foreach var in emtch_specialedbystateguidelines emtch_languageminority ///
		emtch_limitedenglishproficient emtch_skippedgrades emtch_gifted_talentedprogram ///
		emtch_repeatedgrades emtch_childinread_writeinstrctns emtch_childinmathinstrctnsegment {
	    replace `var' = . if inlist(`var', 8, 9)
	    replace `var' = 0 if `var'==5
	    label_indicator, var(`var')    
	}
	rename emtch_specialedbystateguidelines emtch_sped
	rename emtch_limitedenglishproficient emtch_lep
	rename emtch_gifted_talentedprogram emtch_gifted
	rename (emtch_childinread_writeinstrctns emtch_childinmathinstrctnsegment) ///
		(emtch_isreadtchr emtch_ismathteachr)
	foreach var in emtch_readingability emtch_mathability emtch_physicalability {
	    replace `var' = . if inlist(`var', 8, 9)
	}
	rename (emtch_readingability emtch_mathability emtch_physicalability) ///
		(emtch_read_ability emtch_math_ability emtch_physical_ability)
	replace emtch_differentteachers	= . if inlist(emtch_differentteachers, 98, 99)
	rename emtch_differentteachers emtch_numteachers 

	************************************************
	* parent opinions about/perceptions of ability *
	************************************************
	replace pcgc_expectededuc = . if pcgc_expectededuc>8
	foreach var of varlist pcgc_suddenmoodswings-pcgc_worriestoomuch {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	    if "`year'"=="2007" {
	        replace `var' = 4 if `var'==1
		replace `var' = 1 if `var'==3
		replace `var' = 3 if `var'==4
	    }
	}
	replace pcgc_numberoffriends = . if pcgc_numberoffriends>100
	replace pcgc_numberoffriends = . if pcgc_childsage!=1
	rename pcgc_numberoffriends pcgc_childnumfriends
	foreach var of varlist pcgc_cheerful-pcgc_selfreliant {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	}
	
	*******************************************************************************
	*student/school characteristics from the parent/teacher/administrator surveys *
	*******************************************************************************
	replace dem_homeschl = 0 if dem_homeschl==5 
	label values dem_homeschl indicator 
	gen pcgc_grade = pcgc_gradeinsch if pcgc_gradeinsch<98 
	replace pcgc_grade = -1 if pcgc_chldinpresch==1 
	replace pcgc_grade = 0 if pcgc_kindergarten==1 | pcgc_gradeinsch==13
	replace pcgc_grade = 1 if pcgc_firstgrade==1
	replace pcgc_grade = . if pcgc_notinprogram==1
	replace pcgc_grade = . if pcgc_gradeinsch==14
	gen pcgc_chldincollege = pcgc_gradeinsch==14
	if `year'==1997 label var pcgc_grade "Based on Q1G3, Q1G2A-D"
	if `year'==2002 label var pcgc_grade "Based on Q21B5 and Q21B6"
	if `year'==2007 label var pcgc_grade "Based on Q31B6"
	if `year'==1997 label var pcgc_chldincollege "NOT IN 1997"
	if `year'==2002 label var pcgc_chldincollege "Based on Q21B6"
	if `year'==2007 label var pcgc_chldincollege "Based on Q31B6"
	replace pcgc_infedbreakfastprog = . if inlist(pcgc_infedbreakfastprog, 0, 8, 9, 98, 99)
	replace pcgc_infedbreakfastprog = 0 if inlist(pcgc_infedbreakfastprog, 5, 6, 97)
	replace pcgc_infedlunchprog = 5 if pcgc_infedlunchprog==97 
	gen pcgc_frpl= inlist(pcgc_lunchprices, 2, 3) if inlist(pcgc_infedlunchprog, 1, 5) & !inlist(pcgc_lunchprices, 8, 9, 0)
	label values pcgc_frpl indicator
	if `year'==1997 label var pcgc_frpl "Based on Q1G7 and Q1G7A"
	if `year'==2002 label var pcgc_frpl "Based on Q21B9 and Q21B9A"
	if `year'==2007 label var pcgc_frpl "Based on Q31B9 and Q31B9A"
	gen pcgc_privschnow = pcgc_privorpublsch==2 & dem_homeschl==0 if inrange(pcgc_privorpublsch, 1, 3)
	label values pcgc_privschnow indicator
	if `year'==1997 label var pcgc_privschnow "Based on Q1G10 and HOMESCHL"
	if `year'==2002 label var pcgc_privschnow "Based on Q21B11"
	if `year'==2007 label var pcgc_privschnow "Based on Q31B11"
	gen pcgc_privschever = pcgc_privschnow
	replace pcgc_privschever = 1 if pcgc_everattendprivsch==1
	label values pcgc_privschever indicator
	if `year'==1997 label var pcgc_privschever "Based on Q1G10, Q1G10A, and HOMESCHL"
	if `year'==2002 label var pcgc_privschever "Based on Q21B11, Q21B13"
	if `year'==2007 label var pcgc_privschever "Based on Q31B11, Q31B13"
	foreach var in  pcgc_everingiftedprog pcgc_everinspeceduc pcgc_earlyinterventprogs ///
		pcgc_expelled pcgc_repeatgrade {
	replace `var' = . if inlist(`var', 0, 8, 9)
	replace `var' = 0 if `var'==5
	}
	rename pcgc_everingiftedprog pcgc_evergifted
	rename pcgc_everinspeceduc  pcgc_eversped
	rename pcgc_earlyinterventprogs pcgc_headstart
	rename pcgc_expelled pcgc_everexpelled
	rename pcgc_repeatgrade pcgc_everrepeatedgrade
	foreach var in pcgc_infedbreakfastprog pcgc_evergifted  pcgc_eversped ///
		pcgc_headstart pcgc_everexpelled  pcgc_everrepeatedgrade {
	    label_indicator, var(`var')
	}
	replace pcgc_agebegankgartenyrs = . if inlist(pcgc_agebegankgartenyrs, 0, 8, 9)
	rename chld_whatgradeinschool chld_grade
	
	if `year'==1997 replace chld_grade = . if inlist(chld_grade, 98, 99) | chld_grade==0 & chld_ageofchild<5
	gen chld_chldincollege = chld_grade==14 if inrange(chld_grade, 1, 14)
	if `year'==2002 replace chld_grade = . if inlist(chld_grade, 0, 99, 14)
	if `year'==2002 replace chld_grade = 0 if chld_grade==13
	if `year'==2007 replace chld_grade = . if inlist(chld_grade, 13, 14)
	label values chld_grade .
	if `year'==1997 label var chld_chldincollege "NOT IN 1997"
	if `year'==2002 label var chld_chldincollege "Based on Q24B2"
	if `year'==2007 label var chld_chldincollege "Based on Q34B3"
	
	replace emtch_currentgrade = . if inlist(emtch_currentgrade, 98, 99)
	replace emtch_currentgrade = -1 if emtch_currentgrade==1
	replace emtch_currentgrade = 0 if inlist(emtch_currentgrade, 2, 3)
	replace emtch_currentgrade = emtch_currentgrade-3 if inrange(emtch_currentgrade, 4, 11)
	replace emtch_currentgrade = . if inlist(emtch_currentgrade, 12, 13)
	label values emtch_currentgrade .
	replace emtch_currentgrade = -1 if !mi(pstch_daycompleted)
	rename emtch_currentgrade tch_grade
	
	rename pstch_childreninchildsclass pstch_numstudentsinclass
	replace pstch_numstudentsinclass = . if inlist(pstch_numstudentsinclass, 99)
	gen pstch_numteachers = pstch_fulltimeteachers+(0.5*pstch_parttimeteachers) ///
		if !inlist(pstch_fulltimeteachers, 8, 9) &  !inlist(pstch_parttimeteachers, 8, 9)
	gen pstch_stutchratio = pstch_numstudentsinclass/pstch_numteachers
	local label3: variable label pstch_numstudentsinclass
	local label2: variable label pstch_fulltimeteachers
	local label: variable label pstch_parttimeteachers
	label var pstch_stutchratio "`label3'/(`label2'+.5*`label')"
	rename (pstch_numberof*children) (pstch_num*students)
	replace emtch_numstudentsinclass = . if inlist(emtch_numstudentsinclass, 998, 999)
	replace emtch_numfulltimeteachers = . if inlist(emtch_numfulltimeteachers, 98, 99)
	gen emtch_stutchratio = emtch_numstudentsinclass/emtch_numfulltimeteachers
	local label2: variable label emtch_numstudentsinclass
	local label: variable label emtch_numfulltimeteachers
	label var emtch_stutchratio "`label2'/`label'"
	gen tch_numstudentsinclass = emtch_numstudentsinclass
	replace tch_numstudentsinclass = pstch_numstudentsinclass if mi(tch_numstudentsinclass)
	local label: variable label pstch_numstudentsinclass
	label var tch_numstudentsinclass "`label' or `label2'"
	rename pstch_numindianstudents pstch_numamindstudents 
	rename emtch_numamericanindianstudents emtch_numamindstudents 
	rename pstch_numberother pstch_numotherracestudents
	rename emtch_numotherethnicitystudents emtch_numotherracestudents 
	foreach race in white black hispanic asian amind otherrace {
	    gen tch_num`race'students = emtch_num`race'students
	    replace tch_num`race'students = pstch_num`race'students if mi(tch_num`race'students)
	    local label: variable label pstch_num`race'students
	    local label2: variable label emtch_num`race'students
	    label var tch_num`race'students "`label' or `label2'"
	    foreach prefix in pstch emtch tch {
	        replace `prefix'_num`race'students  = . if inlist(`prefix'_num`race'students, 98, 99)
	        gen `prefix'_pctclass_`race' = `prefix'_num`race'students/`prefix'_numstudentsinclass
	        local label: variable label `prefix'_num`race'students
	        local label2: variable label `prefix'_numstudentsinclass
	        label var `prefix'_pctclass_`race' " (`label')/(`label2')"
	    }  
	}
	
	foreach prefix in emtch pstch {
	    replace `prefix'_male_female  = . if inlist(`prefix'_male_female, 8, 9)
	    replace `prefix'_male_female  = 0 if `prefix'_male_female==2
	    rename `prefix'_male_female `prefix'_male
	    label_indicator, var(`prefix'_male)
	    local label_`prefix': variable label `prefix'_male
	}
	gen tch_male = emtch_male
	replace tch_male = pstch_male if mi(tch_male)
	label var tch_male "`label_emtch' and `label_pstch'"

	replace emtch_ethnicity = . if inlist(emtch_ethnicity, 8, 9)
	rename emtch_ethnicity emtch_race
	replace pstch_ethnicity = . if pstch_ethnicity==9
	replace pstch_ethnicity = 6 if pstch_ethnicity==7
	rename pstch_ethnicity pstch_race
	gen tch_race = emtch_race
	replace tch_race = pstch_race if mi(tch_race)
	label values tch_race Q9C6L
	local label: variable label emtch_race
	local label2: variable label pstch_race
	label var tch_race "`label' and `label2'"
	
	foreach var in emtch_monthsatschool emtch_yearsatschool emtch_teachereducation {
	    replace `var' = . if inlist(`var', 98, 99)
	}
	foreach var in pstch_highestlevelofeducation {
	    replace `var' = . if `var'==9
	}
	rename pstch_highestlevelofeducation pstch_teachereducation
	gen tch_teachereducation = emtch_teachereducation
	replace tch_teachereducation = pstch_teachereducation if mi(tch_teachereducation)
	replace emtch_monthsatschool = emtch_monthsatschool/12
	replace emtch_yearsatschool = emtch_yearsatschool+emtch_monthsatschool
	label values emtch_yearsatschool .

	foreach prefix in emtch pstch {
	    gen `prefix'_age = `prefix'_yearcompleted - `prefix'_yearborn ///
		if `prefix'_yearcompleted!=9999 & !inlist(`prefix'_yearborn, 9998, 9999)
	    local label_`prefix'1: variable label `prefix'_yearcompleted
	    local label_`prefix'2: variable label `prefix'_yearborn
	}
	gen tch_age = emtch_age
	replace tch_age = pstch_age if mi(tch_age)
	label var tch_age "`label_emtch1'-`label_emtch2' and `label_pstch1'-`labelpstch2'"
	
	foreach var in emtch_state_localstandardizedtes emtch_teachermadetests emtch_performancebasedassessment {
	    replace `var' = . if inlist(`var', 8, 9)
	}
	rename (emtch_state_localstandardizedtes emtch_teachermadetests emtch_performancebasedassessment) ///
		(emtch_testfreq_stdized emtch_testfreq_teachermade emtch_testfreq_portfolio)
	
	
	gen emadm_publicsch = emadm_schoolcategory==1 if inlist( emadm_schoolcategory, 1, 7) 
	foreach var of varlist emadm_kindergarten-emadm_twelfthgrade {
	    replace `var' = 0 if `var'==5
	}
	egen emadm_numgrades = rowtotal(emadm_kindergarten-emadm_twelfthgrade), missing
	replace emadm_currentstudentenrollment = . if emadm_currentstudentenrollment==9999
	rename emadm_currentstudentenrollment emadm_totalstudentenrollment
	rename emadm_numberindianstudents emadm_numberamindstudents
	rename emadm_numberotherstudents emadm_numberotherracestudents
	local label: variable label emadm_totalstudentenrollment
	local max "9998, 9999"
	foreach race in white black hispanic asian amind otherrace {
	    if "`race'"=="asian" local max "998, 999"
	    replace emadm_number`race'students = . if inlist(emadm_number`race'students, `max')
	    rename emadm_number`race'students emadm_num`race'students
	    gen emadm_schpct`race'students = emadm_num`race'students/emadm_totalstudentenrollment
	    local label_race: variable label emadm_num`race'students
	    label var emadm_schpct`race'students "`label_race'/`label'"
	    
	}
	gen emadm_schnumfrpl = emadm_numbfreelunchprog+emadm_numbsubsidizedlunchprog  ///
		if emadm_numbfreelunchprog!=9999 & emadm_numbsubsidizedlunchprog!=999
	gen emadm_schpctfrpl = emadm_schnumfrpl/emadm_totalstudentenrollment
	local label_frpl1: variable label emadm_numbfreelunchprog
	local label_frpl2: variable label emadm_numbsubsidizedlunchprog
	label var emadm_schpctfrpl "`label_frpl1' + `label_frpl2'/`label'"
	
	rename emadm_breakfastprovisions emadm_schfedbreakfastprogram
	replace emadm_schfedbreakfastprogram = . if emadm_schfedbreakfastprogram==9
	replace emadm_schfedbreakfastprogram = 0 if emadm_schfedbreakfastprogram==5
	label_indicator, var(emadm_schfedbreakfastprogram)
	
	replace emadm_studentratio = . if emadm_studentratio==99
	rename emadm_studentratio emadm_schstutchratio
	replace emadm_dollarsspent_student = . if inlist(emadm_dollarsspent_student, 99998, 99999)
	rename emadm_dollarsspent_student emadm_perpupilexpenditure
	
	replace psadm_numberofchildren = . if psadm_numberofchildren==999
	rename psadm_numberofchildren psadm_totalstudentenrollment
	rename (psadm_numberchildrenwhite_nonhis psadm_numberchildrenblack_nonhis ///
		psadm_numberchildrenhispanic psadm_numberchildrenasian ///
		psadm_numberchildrenamericanindi psadm_numberchildrenother) ///
		(psadm_numberwhitestudents psadm_numberblackstudents psadm_numberhispanicstudents ///
		psadm_numberasianstudents psadm_numberamindstudents psadm_numberotherracestudents)
	local label: variable label psadm_totalstudentenrollment
	local max "998, 999"
	foreach race in white black hispanic asian otherrace amind {
	    if "`race'"=="hispanic" local max "98, 99"
	    if "`race'"=="amind" local max "8, 9"
	    replace psadm_number`race'students = . if inlist(psadm_number`race'students, `max')
	    rename psadm_number`race'students psadm_num`race'students
	    gen psadm_schpct`race'students = psadm_num`race'students/psadm_totalstudentenrollment
	       local label_race: variable label psadm_num`race'students
	    label var psadm_schpct`race'students "`label_race'/`label'"
	}
	replace psadm_studentratio = . if psadm_studentratio==99
	rename psadm_studentratio psadm_schstutchratio
	replace psadm_dollarsspentperstudent = . if inlist(psadm_dollarsspentperstudent, 9998, 9999)
	rename psadm_dollarsspentperstudent psadm_perpupilexpenditure

	*************************************************************
	* birth and health variables and learning disabilities etc. *
	*************************************************************
	replace pcgc_heightmeasured = . if inlist(pcgc_heightmeasured, 98, 99)
	replace pcgc_heightmeasured = pcgc_estheightlastdocvisit if mi(pcgc_heightmeasured) & !inlist(pcgc_estheightlastdocvisit, 98, 99)
	replace pcgc_heightmeasured = pcgc_estcurrentheight if mi(pcgc_heightmeasured) & !inlist(pcgc_estcurrentheight, 98, 99)
	rename pcgc_heightmeasured pcgc_childheight 
	
	replace pcgc_weightmeasured = . if inlist(pcgc_weightmeasured, 998, 999)
	replace pcgc_weightmeasured = pcgc_estweightlastdocvisit if mi(pcgc_weightmeasured) & !inlist(pcgc_estweightlastdocvisit, 998, 999)
	replace pcgc_weightmeasured = pcgc_estcurrentweight if mi(pcgc_weightmeasured) & !inlist(pcgc_estcurrentweight, 998, 999)
	rename pcgc_weightmeasured pcgc_childweight
	if `year'==1997 label var pcgc_childweight "Q1A3 and Q1A3A"

	foreach var in pcgc_neonatalintnsvcare pcgc_brfeedasinfant pcgc_medicaidpaidbillspregnant {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	    replace `var' = 0 if `var' == 5
	    label_indicator, var(`var')
	}
	rename (pcgc_neonatalintnsvcare pcgc_brfeedasinfant pcgc_medicaidpaidbillspregnant) ///
		(pcgc_nicu pcgc_brstfd pcgc_mcaidatbirth)
	gen pcgc_govtasstatbirth = 1 if pcgc_wicprogrampregnant==1 | ///
		pcgc_foodstampspregnant==1 |  pcgc_freefoodpregnant==1 | ///
		pcgc_adcafdcpregnant==1 |  pcgc_otherhelppregnant==1
	replace pcgc_govtasstatbirth = 0 if pcgc_wicprogrampregnant==5 & ///
		pcgc_foodstampspregnant==5 & pcgc_freefoodpregnant==5 & ///
		pcgc_adcafdcpregnant==5 & pcgc_otherhelppregnant==5
	if `year'==1997 label var pcgc_govtasstatbirth "Based on Q1A15-Q1A19"
	if `year'==2002 label var pcgc_govtasstatbirth "NOT IN 2002"
	label values pcgc_govtasstatbirth indicator
	
	foreach var in pcgc_asthma pcgc_speechimpairment pcgc_hearingdifficulty ///
		pcgc_difficultyseeing pcgc_retardation pcgc_orthopedimpairmt ///
		pcgc_developmentdelay pcgc_learningdisability pcgc_autism ///
		pcgc_hyperactivity pcgc_othchroniccond pcgc_drvisitemotprob {
	    replace `var' = . if inlist(`var', 8, 9)
	    replace `var' = 0 if `var'==5
	    label_indicator, var(`var')
	}
	rename pcgc_drvisitemotprob pcgc_sawpsych
	replace pcgc_pcgratedchildhlth = . if inlist(pcgc_pcgratedchildhlth, 8, 9) 

	***********************
	* parenting practices *
	***********************
	replace pcgc_fathersresidence = . if pcgc_fathersresidence>2
	replace pcgc_fathersresidence = 0 if pcgc_fathersresidence==2
	label_indicator, var(pcgc_fathersresidence)
	rename pcgc_fathersresidence pcgc_fatherinhh
	replace pcgh_livew_spouseorpartner = pcgh_livew_spouseorpartner - 1
	label_indicator, var(pcgh_livew_spouseorpartner)
	rename pcgh_livew_spouseorpartner pgch_pcglivewspouseorparter
	replace pcgc_weekdaytv = . if pcgc_weekdaytv>24
	replace pcgc_weekendtv = 1 if pcgc_weekendtv==96
	replace pcgc_weekendtv = . if pcgc_weekendtv>24
	
	replace pcgc_everspanked = . if inlist(pcgc_everspanked, 8, 9)
	replace pcgc_everspanked = 0 if pcgc_everspanked==5
	label_indicator, var(pcgc_everspanked)
	replace pcgc_readtochild = . if inlist(pcgc_readtochild, 8, 9)
	foreach var in pcgc_booksread pcgc_artsandcrafts pcgc_playedsports ///
		pcgc_builtsomething pcgc_videogames pcgc_homework pcgc_boardgames {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	    rename `var' `var'withchild
	}
	rename pcgc_booksreadwithchild pcgc_readtochild2
	foreach var in pcgc_shownphysicalaffection pcgc_saidiloveyou ///
		pcgc_participateinactivities pcgc_jokedorplayed pcgc_talkaboutinterests ///
		pcgc_spokenappreciatively   {
	    replace `var' = . if inlist(`var', 8, 9)
	    rename `var' `var'
	}
	foreach var in pcgc_hostile_loving pcgc_pride_nopride pcgc_warmth_nowarmth {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	    local label: variable label `var'
	    local label2: variable label pcgc_childpcgtogether
	    label variable `var' "`label' (if `label2')"
	}
	rename (pcgc_hostile_loving pcgc_pride_nopride pcgc_warmth_nowarmth) ///
	(pcg_hostilelovingscale pcg_prideinchildscale pcg_warmthtochildscale) 
		
	foreach var in pcgh_receivedhelp pcgh_providedhelp pcgh_receiveemotionalsupport pcgh_providedemotionalsupport pcgh_receivenewspaper {
	    replace `var' = . if inlist(`var', 8, 9)
	    replace `var' = 0 if `var'==5
	    label_indicator, var(`var')
	}
	rename (pcgh_receivedhelp pcgh_providedhelp pcgh_receiveemotionalsupport pcgh_providedemotionalsupport) ///
		(pcgh_recchildorhousehelp pcgh_provchildorhousehelp pcgh_recemotionalsupport pcgh_provemotionalsupport)
		
	foreach var in pcgh_mostimportquality pcgh_secondimportquality pcgh_thirdimportquality pcgh_fourthimportquality {  
	replace `var' = . if inlist(`var', 0, 8, 9)
	rename `var' `var'forlife
	}

	foreach var in pcgh_amountofrules pcgh_rulesstrictlyenforced {
	    replace `var' = . if inlist(`var', 8, 9)
	    replace `var' = 0 if `var'==2
	    label_indicator, var(`var')
	}
	rename pcgh_amountofrules pcgh_lotsofhhrules
	foreach var in pcgh_limitsontv pcgh_limitsonkindsoftv pcgh_notvduringmeals ///
		pcgh_limitsonbedtime pcgh_limitsonsweets pcgh_trytocontrolwhochildiswith ///
		pcgh_controlchilddoesafterschl pcgh_sethomeworktime  ///
		pcgh_discussruleswithchild  { // note questions only asked if there's a child over 3 in the hh
			replace `var' = . if inlist(`var', 0, 8, 9)
			replace `var' = 0 if inlist(`var', 4, 5)
			replace `var' = 1 if inlist(`var', 2, 3)
			label_indicator, var(`var')
		}

	foreach var in pcgh_notquestionteachingmethods ///
		pcgh_kidsshouldallbetreatedsame pcgh_kidsshouldalwaysobeyteacher ///
		pcgh_kidstreatedasfriendsbyadult pcgh_naturalabilitysuccessinsch ///
		pcgh_hardworksuccessinsch pcgh_beinglikedbyothsuccessinsch ///
		pcgh_decisionsmadebymanofhh pcgh_husb_wifesharetasks pcgh_womenhappierathome ///
		pcgh_workiseithermensorwomens pcgh_betterifmanearnsliving ///
		pcgh_betterwifehelphusbcareer pcgh_employmomequalunemploymom ///
		pcgh_independindaughtersandsons pcgh_preschchildsuffersifmomempl ///
		pcgh_dadhoodmostfulfilexper pcgh_momhoodmostfulfilexper ///
		pcgh_dadsshouldinteractw_child pcgh_dadasinvolvedasmom pcgh_dadandmomequal { 
	replace `var' = . if inlist(`var', 0, 8, 9)
	}
	rename pcgh_beinglikedbyothsuccessinsch pcgh_likedbyothsuccessinsch
	rename (pcgh_*successinsch) (pcgh_successinsch_*)
	rename pcgh_husb_wifesharetasks pcgh_husbwifesharetasks
	rename pcgh_dadsshouldinteractw_child pcgh_dadsshouldinteractwchild
	
	foreach var in pcgh_familyfightsalot pcgh_familycriticizes {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	    
	    if `year'==1997 {
	        replace `var' = 1 if inlist(`var', 1, 2)
	        replace `var' = 0 if inlist(`var', 3, 4)
	    }
	    if inlist(`year', 2002, 2007) {
	        replace `var' = 1 if inlist(`var', 4, 5)
	        replace `var' = 0 if inlist(`var', 1, 2, 3)
	    }
	    label_indicator, var(`var')
	}
	rename pcgh_familycriticizes pcgh_familyoftencriticizes
		
	
	***************************************
	* pcg self-confidence/esteem measures *
	***************************************
	foreach var in pcgc_rosenbergselfesteemscale pcgc_pearlinselfefficacyscalesho ///
		pcgc_agginparentingscaleshort { // short is comparable with 2002, full is not
	replace `var' = . if inlist(`var', 0, 9)
	}
	replace pcgc_distressscalek6 = . if pcgc_distressscalek6==99 // k6 is comparable with 2002, k10 is not
	rename (pcgc_rosenbergselfesteemscale pcgc_pearlinselfefficacyscalesho ///
		pcgc_agginparentingscaleshort pcgc_distressscalek6) ///
		(pcgc_ownselfesteemscore pcgc_ownselfefficscore ///
		pcgc_aggravationinparentingscore pcgc_owndistressscore)
	* pcgh_personofworth-pcgh_thinkimnogood make up self esteem scale
	* pcgh_cantsolveprobs-pcgh_candolittletochangelife make up self efficacy scale
	* pcgh_tiredfornoreason-pcgh_worthless make up parent distress scale
	* pcgc_hardertocare-pcgc_dobetterw_outchild and pcgh_beingparentishard pcgh_feeltiredraisingfamily are aggravation in parenting scale 
	** these descriptions are the full versions, switched to the short versions to be comparable across waves! 
	foreach var in pcgh_personofworth pcgh_goodqualities pcgh_feellikeafailure ///
		pcgh_dothingswell pcgh_notmuchtobeproudof pcgh_positiveattitude ///
		pcgh_satisfiedwithself pcgh_wantmorerespect pcgh_feeluselessattimes ///
		pcgh_thinkimnogood pcgh_cantsolveprobs pcgh_pushedaround pcgh_littlecontrol ///
		pcgh_candoanything pcgh_feelhelpless pcgh_futuredependsonme ///
		pcgh_candolittletochangelife {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	    if `year'==1997 {
	        replace `var' = 1 if inlist(`var', 1, 2)
	        replace `var' = 0 if inlist(`var', 3, 4)
	    }
	    if inlist(`year', 2002, 2007) {
	    	replace `var' = 0 if inlist(`var', 1, 2)
	        replace `var' = 1 if inlist(`var', 3, 4)
	    }
	    label_indicator, var(`var')
	}
	
	foreach var in pcgh_tiredfornoreason pcgh_nervous pcgh_sonervouscantcalm ///
		pcgh_hopeless pcgh_restless pcgh_sorestlesscantsitstill pcgh_depressed ///
		pcgh_everythinganeffort pcgh_sosadcouldntbecheered pcgh_worthless {
	    if inlist(`year', 1997, 2002) {
	        replace `var' = . if inlist(`var', 8, 9)
	        replace `var' = 1 if inlist(`var', 1, 2, 3)
	        replace `var' = 0 if inlist(`var', 4, 5)
	    }
	    if `year'==2007 {
	        replace `var' = . if inlist(`var', 0, 8, 9)
	        replace `var' = 0 if inlist(`var', 1, 2)
	       replace `var' = 1 if inlist(`var', 3, 4, 5)
	   }
	label_indicator, var(`var')
	}
	
	foreach var in pcgc_hardertocare pcgc_bothersome pcgc_givinguplife pcgc_feelangrywchild ///
		pcgc_dobetterw_outchild pcgh_beingparentishard pcgh_feeltrappedasparent ///
		pcgh_childrenarework pcgh_feeltiredraisingfamily {
	    replace `var' = . if inlist(`var', 8, 9)
	    replace `var' = 0 if `var'==1
	    replace `var' = 1 if inlist(`var', 2, 3, 4, 5)
	    label_indicator, var(`var')
	}
	rename pcgc_dobetterw_outchild pcgc_dobetterwithoutchild

	********************************
	* neighborhood characteristics *
	********************************
	replace pcgh_neighborhoodrating = . if inlist(pcgh_neighborhoodrating, 8, 9)
	replace pcgh_familymembersinneighborhood = . if pcgh_familymembersinneighborhood>3000
	foreach var in  pcgh_goodfriendsinneighborhood pcgh_childrenyouknowbyname {
	    replace `var' = . if inlist(`var', 998, 999)
	}
	foreach var in pcgh_proportionownhouse pcgh_proportionhispanic_latino ///
		pcgh_proportionasian pcgh_proportionwhite pcgh_proportionafricanamerican {
		replace `var' = . if inlist(`var', 8, 9)
		}
	rename (pcgh_proportionownhouse pcgh_proportionhispanic_latino ///
		pcgh_proportionasian pcgh_proportionwhite pcgh_proportionafricanamerican) ///
		(pcgh_propnbhdownhouse pcgh_propnbhdhisp pcgh_propnghdasian  ///
		pcgh_propnbhdwhite pcgh_propnbhdblack)
	foreach var in pcgh_amountattendreligiousservic pcgh_importanceofreligion pcgh_safetowalkaround {
	    replace `var' = . if inlist(`var', 0, 8, 9)
	}
	rename pcgh_amountattendreligiousservic pcgh_attendreligservices

	***********************************************************
	* family characteristics unrelated to parenting practices *
	***********************************************************
	foreach var in pcgh_healthinsuremployer pcgh_healthinsurprivateplan ///
		pcgh_healthinsurmedicare pcgh_healthinsuranothersource pcgh_healthinsurnotatall {
	    replace `var' = . if inlist(`var', 8, 9)
	    replace `var' = 0 if `var'==5
	    label_indicator, var(`var')
	}
	egen temp = rowtotal(pcgh_healthinsuremployer pcgh_healthinsurprivateplan ///
		pcgh_healthinsurmedicare pcgh_healthinsuranothersource pcgh_healthinsurnotatall)
	assert pcgh_healthinsurnotatall==0 if temp>1
	gen pcgh_hashealthinsurance = 1-pcgh_healthinsurnotatall
	if `year'==1997 label variable pcgh_hashealthinsurance "Based on Q2A50_1-Q2A50_5"
	rename pcgh_healthinsurmedicare pcgh_mcaidormcare
	drop temp
	replace pcgh_moneyleftatendofmonth = . if inlist(pcgh_moneyleftatendofmonth, 8, 9)
	foreach var in pcgh_actvtsouthome_working pcgh_actvtsouthome_school_colleg  ///
		pcgh_actvtsouthome_intrainingpro pcgh_actvtsouthome_lookingforwor ///
		pcgh_actvtsouthome_volunteerwork pcgh_actvtsouthome_recreatnlacti ///
		pcgh_actvtsouthome_other {
	    replace `var' = . if inlist(`var', 8, 9)
	    replace `var' = 0 if `var'==5
	    label_indicator, var(`var')	
	}
	rename (pcgh_actvtsouthome_working pcgh_actvtsouthome_school_colleg  ///
		pcgh_actvtsouthome_intrainingpro pcgh_actvtsouthome_lookingforwor ///
		pcgh_actvtsouthome_volunteerwork) ///
		(pcgh_isworking pcgh_isinschoolcolleg pcgh_isintrainingprog ///
		pcgh_islookingforwork pcgh_isvolunteering)
	
	************************** 
	* KEEP CLEANED VARIABLES *
	**************************
	#delimit ;
	local identifiers uid ER30001 ER30002 cds_int_num_`year' cds_seq_num_`year' dem_ch*prwt;
	local interviews haschiw moyr_chiw haspcgciw haspcghiw moyr_pcgiw 
		hasemtchiw moyr_emtchiw haspstchiw moyr_pstchiw hashbcpiw moyr_hbcpiw
		hasemadmiw haspsadmiw;
	local demographics dem_ageatpcg dem_birthmoyr dem_chrace dem_childofheads 
		dem_immigfam dem_immigchild dem_childofheads pcgc_foodinsecure dem_sblnum;
	local child_ability_assess chld_spanishassessment chld_read_lw_raw chld_read_pc_raw
		chld_math_cal_raw chld_math_ap_raw chld_digitspanforward_raw
		chld_digitspanbackward_raw chld_digitspantotal_raw chld_read_lw_ss
		chld_read_pc_ss chld_read_total_ss chld_math_cal_ss chld_math_ap_ss
		chld_math_total_ss chld_read_lw_ws chld_read_pc_ws chld_read_total_ws
		chld_math_cal_ws chld_math_ap_ws chld_math_total_ws chld_read_lw_pctile
		chld_read_pc_pctile chld_math_cal_pctile chld_math_ap_pctile 
		chld_read_total_pctile chld_math_total_pctile  ;
	local child_self_conf chld_mathcnf_rateownskill chld_mathcnf_skillreltopeers
		chld_mathcnf_skillrelothersubj chld_mathcnf_expectedperf
		chld_mathcnf_goodatlearnnew chld_mathcnf_howhard chld_mathcnf_howuseful
		chld_mathcnf_imptobegood chld_mathcnf_interesting chld_mathcnf_enjoy
		chld_readcnf_rateownskill chld_readcnf_skillreltopeers 
		chld_readcnf_skillrelothersubj chld_readcnf_expectedperf 
		chld_readcnf_goodatlearnnew chld_readcnf_howhard chld_readcnf_howuseful
		chld_readcnf_imptobegood chld_readcnf_interesting chld_readcnf_enjoy
		chld_gencnf_doimportantthings chld_gencnf_likebeingmyself chld_gencnf_lottobeproudof 
		chld_gencnf_dothingsaswellasoth chld_gencnf_goodthingsaboutme chld_gencnf_goodasothers 
		chld_gencnf_othersthinkiamgood chld_gencnf_dothingswell chld_mathcnf_selfconcept
		chld_readcnf_selfconcept chld_gencnf_selfconcept chld_worrynogoodjob
		chld_discouragedaboutfuture chld_worryaboutmoney chld_feelpartofschool
		chld_feelclosetopeers chld_happyatschool chld_safeatschool chld_skinproblems
		chld_expectededuc;
	local parent_ability pcgc_pcrawscore;
	local parent_percep_ability pcgc_expectededuc pcgc_suddenmoodswings-pcgc_worriestoomuch 
		pcgc_childnumfriends pcgc_cheerful-pcgc_selfreliant;
	local tch_percep_ability emtch_expectededuc tch_suddenmoodswings-tch_excessivedemandoftchr
		emtch_academicunderachiever emtch_goesthroughthemotions emtch_withdrawnfromactivities
		emtch_actsupinclass tch_academiccompetence tch_socialcompetence tch_physicalcompetence
		emtch_read_ability emtch_math_ability emtch_physical_ability
		emtch_sped emtch_languageminority emtch_lep
		emtch_gifted emtch_skippedgrades emtch_repeatedgrades emtch_numteachers
		emtch_isreadtchr emtch_ismathteachr;
	local student_school_chars dem_homeschl pcgc_grade chld_grade tch_grade pcgc_notinprogram
		pcgc_infedbreakfastprog pcgc_frpl pcgc_privschnow pcgc_privschever 
		pcgc_evergifted  pcgc_eversped pcgc_headstart pcgc_everexpelled
		pcgc_everrepeatedgrade pcgc_agebegankgartenyrs emtch_sped 
		emtch_languageminority emtch_lep emtch_gifted emtch_skippedgrades 
		emtch_repeatedgrades emtch_numteachers emtch_isreadtchr 
		emtch_ismathteachr tch_numstudentsinclass emtch_stutchratio 
		pstch_stutchratio tch_pctclass_* tch_male tch_race tch_teachereducation
		emtch_yearsatschool tch_age emtch_testfreq_stdized emtch_testfreq_teachermade
		emtch_testfreq_portfolio emadm_publicsch emadm_numgrades emadm_totalstudentenrollment
		emadm_schpctwhitestudents emadm_schpctblackstudents emadm_schpcthispanicstudents
		emadm_schpctasianstudents emadm_schpctamindstudents emadm_schpctotherracestudents
		emadm_schpctfrpl emadm_schfedbreakfastprogram emadm_schstutchratio
		emadm_perpupilexpenditure psadm_totalstudentenrollment psadm_schpctwhitestudents
		psadm_schpctblackstudents psadm_schpcthispanicstudents psadm_schpctasianstudents 
		psadm_schpctamindstudents psadm_schpctotherracestudents psadm_schstutchratio 
		psadm_perpupilexpenditure;
	local birth_health pcgc_childheight pcgc_childweight pcgc_nicu pcgc_brstfd pcgc_mcaidatbirth
		pcgc_govtasstatbirth pcgc_asthma pcgc_speechimpairment pcgc_hearingdifficulty
		pcgc_difficultyseeing pcgc_retardation pcgc_orthopedimpairmt pcgc_developmentdelay
		pcgc_learningdisability pcgc_autism pcgc_hyperactivity pcgc_othchroniccond 
		pcgc_sawpsych pcgc_pcgratedchildhlth;
	local parenting_prac pcgc_fatherinhh pcgc_weekdaytv pcgc_weekendtv pcgc_everspanked
		pcgc_readtochild pcgc_readtochild2 pcgc_artsandcraftswithchild
		pcgc_playedsportswithchild pcgc_builtsomethingwithchild pcgc_videogameswithchild 
		pcgc_homeworkwithchild pcgc_boardgameswithchild pcgc_shownphysicalaffection
		pcgc_saidiloveyou pcgc_participateinactivities pcgc_jokedorplayed 
		pcgc_talkaboutinterests pcgc_spokenappreciatively  pcg_hostilelovingscale 
		pcg_prideinchildscale pcg_warmthtochildscale pcgh_recchildorhousehelp 
		pcgh_provchildorhousehelp pcgh_recemotionalsupport pcgh_provemotionalsupport 
		pcgh_mostimportqualityforlife pcgh_secondimportqualityforlife 
		pcgh_thirdimportqualityforlife pcgh_fourthimportqualityforlife pcgh_lotsofhhrules
		pcgh_rulesstrictlyenforced pcgh_receivenewspaper pcgh_limitsontv pcgh_limitsonkindsoftv
		pcgh_notvduringmeals pcgh_limitsonbedtime pcgh_limitsonsweets 
		pcgh_trytocontrolwhochildiswith pcgh_controlchilddoesafterschl pcgh_sethomeworktime
		pcgh_discussruleswithchild pcgh_notquestionteachingmethods 
		pcgh_kidsshouldallbetreatedsame pcgh_kidsshouldalwaysobeyteacher
		pcgh_kidstreatedasfriendsbyadult pcgh_successinsch_naturalability 
		pcgh_successinsch_hardwork pcgh_successinsch_likedbyoth pcgh_decisionsmadebymanofhh 
		pcgh_husbwifesharetasks pcgh_womenhappierathome pcgh_workiseithermensorwomens 
		pcgh_betterifmanearnsliving pcgh_betterwifehelphusbcareer 
		pcgh_employmomequalunemploymom pcgh_independindaughtersandsons
		pcgh_preschchildsuffersifmomempl pcgh_dadhoodmostfulfilexper 
		pcgh_momhoodmostfulfilexper pcgh_dadsshouldinteractwchild pcgh_dadasinvolvedasmom 
		pcgh_dadandmomequal pcgh_attendreligservices pcgh_importanceofreligion;
	local parent_own_conf pcgc_ownselfesteemscore pcgc_ownselfefficscore
		pcgc_aggravationinparentingscore pcgc_owndistressscore
		pcgh_personofworth pcgh_goodqualities pcgh_feellikeafailure pcgh_dothingswell
		pcgh_notmuchtobeproudof pcgh_positiveattitude pcgh_satisfiedwithself 
		pcgh_wantmorerespect pcgh_feeluselessattimes pcgh_thinkimnogood pcgh_cantsolveprobs
		pcgh_pushedaround pcgh_littlecontrol pcgh_candoanything pcgh_feelhelpless
		pcgh_futuredependsonme pcgh_candolittletochangelife pcgh_tiredfornoreason pcgh_nervous
		pcgh_sonervouscantcalm pcgh_hopeless pcgh_restless pcgh_sorestlesscantsitstill 
		pcgh_depressed pcgh_everythinganeffort pcgh_sosadcouldntbecheered pcgh_worthless 
		pcgc_hardertocare pcgc_bothersome pcgc_givinguplife pcgc_feelangrywchild 
		pcgc_dobetterwithoutchild pcgh_beingparentishard pcgh_feeltrappedasparent 
		pcgh_childrenarework pcgh_feeltiredraisingfamily;
	local neighborhood pcgh_neighborhoodrating pcgh_familymembersinneighborhood
		pcgh_goodfriendsinneighborhood pcgh_childrenyouknowbyname pcgh_propnbhdownhouse
		pcgh_propnbhdhisp pcgh_propnghdasian pcgh_propnbhdwhite pcgh_propnbhdblack;
	local other_fam_chars pcgh_hashealthinsurance pcgh_mcaidormcare pcgh_moneyleftatendofmonth
		pcgh_isworking pcgh_isinschoolcolleg pcgh_isintrainingprog pcgh_islookingforwork
		pcgh_isvolunteering;
	
	keep `identifiers' `interviews' `demographics' `child_ability_assess' `child_self_conf' 
		`parent_ability' `parent_percep_ability' `tch_percep_ability' `student_school_chars' 
		`birth_health' `parenting_prac' `parent_own_conf' `neighborhood' `other_fam_chars';
	
	gen year = `year';
	order year `identifiers' `interviews' `demographics' `child_ability_assess' 
		`child_self_conf' `parent_ability' `parent_percep_ability' `tch_percep_ability' 
		`student_school_chars' `birth_health' `parenting_prac' `parent_own_conf' 
		`neighborhood' `other_fam_chars';

	foreach var of varlist `demographics' `child_ability_assess' 
		`child_self_conf' `parent_ability' `parent_percep_ability' `tch_percep_ability' 
		`student_school_chars' `birth_health' `parenting_prac' `parent_own_conf' 
		`neighborhood' `other_fam_chars' {;
	    local shortname = substr("`var'", 1, 27);
	    local l_`shortname'_`W': variable label `var';
	    local l_`shortname'_`W' = subinstr("`l_`shortname'_`W''", "NOT IN `year' and NOT IN `year' and NOT IN `year'", "NOT IN `year'", .);
	    local l_`shortname'_`W' = subinstr("`l_`shortname'_`W''", "NOT IN `year' and NOT IN `year'", "NOT IN `year'", .);
	    local l_`shortname'_`W' = subinstr("`l_`shortname'_`W''", "NOT IN `year' or NOT IN `year'", "NOT IN `year'", .);
	    local l_`shortname'_`W' = subinstr("`l_`shortname'_`W''", "NOT IN `year'/NOT IN `year'", "NOT IN `year'", .);
	    
	};
	
	#delimit cr
	
	tempfile cds`W'
	save `cds`W'', replace	


local W = `W'+1
}

* bring in the caregiver idetifiers
do "`RAW'/CDS1997/M265631.do" 
foreach type in PCG OCG {
    foreach yr in 97 02 07 {
    if `yr'>10 local year = 1900+`yr'
    else local year = 2000+`yr'
    gen `type'_`year'_uid = string(`type'ID_`yr', "%04.0f") + string(`type'PN_`yr', "%03.0f")
    }
}  
gen uid = string(ER30001, "%04.0f") + string(ER30002, "%03.0f")
keep *uid*
tempfile caregiverids
save `caregiverids'

* append together the main cds files
use `cds1', clear
append using `cds2'
append using `cds3'

* label with varnumbers from all years for tracking
foreach var of varlist `demographics' `child_ability_assess' ///
		`child_self_conf' `parent_ability' `parent_percep_ability' `tch_percep_ability' /// 
		`student_school_chars' `birth_health' `parenting_prac' `parent_own_conf' ///
		`neighborhood' `other_fam_chars' {
	    local shortname = substr("`var'", 1, 27)
	    label variable `var' "`l_`shortname'_1', `l_`shortname'_2', `l_`shortname'_3' "
	}
	label var dem_birthmoyr "Moth-year of birth (moyr_pcgiw - dem_ageatpcg)"
	label var haspcghiw "Has PCG HH interview in this year"
	label var haspcgciw "Has PCG child interview in this year"
	label var haschiw "Has child interview in this year"
	label var hasemtchiw "Has elem/mid teacher interview in this year"
	label var haspstchiw "Has presch teacher interview in this year"
	label var hashbcpiw "Has home-based care provider interview in this year"
	label var hasemadmiw "Has elem/mid admin interview in this year"
	label var haspsadmiw "Has presch admin interview in this year"
	foreach var in moyr_chiw moyr_pcgiw moyr_emtchiw moyr_pstchiw moyr_hbcpiw {
	    label var `var' "Month-year of interview"
	}
	
* format dates
format *moyr* %tm

* counting observations etc.
bys uid: gen count_uid = _N
bys uid: egen count_chiw = total(haschiw)
gen haschandtchiw =  haschiw==1 & (hasemtch==1 | haspstch==1)
bys uid: egen count_chandtchiw = total(haschandtchiw)

* add the caregiver idetifiers and add information about them (for now, not adding pcg info other than household info so don't really need these)
merge m:1 uid using `caregiverids', assert(3) keep(3) nogen 

* add gender from the individual cross-year file
merge m:1 uid using `indiv_cross_year', assert(2 3) keep(3) nogen
gen dem_chfemale = ER32000==2 if !mi(ER32000)
drop ER32000

* reordering
order uid year ER3* cds_int_num_* cds_seq_num* , first
order haschandtchiw count_* , after(haspsadmiw)
order dem_chfemale, before(dem_chrace) 

sort ER30001 ER30002 year

replace year = 2003 if year==2002


********************************************************************************
// Further cleaning of important variables

* expected education levels
foreach pref in chld pcgc emtch {
    gen `pref'_expectsatleastsomecollege = `pref'_expectededuc >=4 if !mi(`pref'_expectededuc)
    gen `pref'_expects4yrcollegedeg = `pref'_expectededuc >=6 if !mi(`pref'_expectededuc)
    gen `pref'_expectsmorethancollege = `pref'_expectededuc >=7 if !mi(`pref'_expectededuc)
} 

* race
bys uid: egen temp = max(dem_chrace) // only reported in 1997
drop dem_chrace
rename temp dem_chrace
local racelist white black hisp 

forval i = 1(1)3 {
    local race: word `i' of `racelist'
    gen chld_`race' = dem_chrace==`i' if !mi(dem_chrace)
}

* qob & yob
gen temp = subinstr(string(dem_birthmoyr, "%tm"), "m", " ", 1)
split temp, destring
drop temp
rename temp1 dem_birthyear
rename temp2 dem_birthmonth
gen dem_birthquarter = 1 if inrange(dem_birthmonth, 1, 3)
replace dem_birthquarter = 2 if inrange(dem_birthmonth, 4, 6)
replace dem_birthquarter = 3 if inrange(dem_birthmonth, 7, 9)
replace dem_birthquarter = 4 if inrange(dem_birthmonth, 10, 12)

* acne
gen chld_hasacne = inrange(chld_skinproblems, 3, 6) if !mi(chld_skinproblems)
gen chld_hassevereacne = inrange(chld_skinproblems, 5, 6) if !mi(chld_skinproblems)

* parenting practices
foreach var in pcgc_everspanked pcgc_brstfd {
    gen temp = `var'==1
    drop `var'
    bys uid: egen `var' = max(temp)
    drop temp
}

rename pcgc_readtochild2 pcgc_readtochildwithchild
foreach var in pcgc_readtochildwithchild pcgc_artsandcraftswithchild pcgc_playedsportswithchild ///
	pcgc_homeworkwithchild pcgc_boardgameswithchild {
    replace `var' = 0 if inlist(`var', 1, 2, 3)
    replace `var' = 1 if inlist(`var', 4, 5)
    local newname = subinstr("`var'", "withchild", "morethan1perwk", .)
    rename `var' `newname'
    }
   
foreach var in pcgc_shownphysicalaffection pcgc_saidiloveyou {
    replace `var' = 0 if inlist(`var', 1, 2, 3)
    replace `var' = 1 if inlist(`var', 4, 5)
}
rename (pcgc_shownphysicalaffection pcgc_saidiloveyou) (pcgc_physaffectmorethan1perwk pcgc_saidlovemorethan1perwk)

replace pcgc_spokenappreciatively = 0 if inlist(pcgc_spokenappreciatively, 1, 2, 3, 4)
replace pcgc_spokenappreciatively = 1 if pcgc_spokenappreciatively==5
rename pcgc_spokenappreciatively pcgc_appreciateeveryday

local qualitylist obey popular thinkforself workhard helpothers
forval i = 1/5 {
    local qual: word `i' of `qualitylist'
    gen temp = pcgh_mostimportqualityforlife==`i' if !mi(pcgh_mostimportqualityforlife)
    bys uid: egen pcgh_mostimpqual_`qual' = max(temp)
    drop temp
}

* student characteristics
foreach var in pcgc_evergifted pcgc_eversped pcgc_everrepeatedgrade  {
    replace `var' = 0 if pcgc_grade<0 | pcgc_notinprogram==1
}
replace pcgc_everrepeatedgrade = 0 if pcgc_grade==0
	
save "`CLEAN'/cleaned_cds.dta", replace 



